alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (exceptions-nest.scala)

This example Scala source code file (exceptions-nest.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

app, e1, e1, e2, e2, e3, exception, exception, int, nullpointerexception, nullpointerexception, ok, string, test

The Scala exceptions-nest.scala source code

object Test extends App {

  println(test1)
  println(test2)
  println(test3)
  println(test4)
  println(test5)
  try { println(test6) } catch { case _ => println("OK") }
  println(test7)
  try { println(test8) } catch { case _ => println("OK") }
  println(test9)
  println(test10)
  println(test11)
  println(test12)

  def test1 = {
    var x = 1
    try {
      x = 2
    } catch {
      case _: NullPointerException => x = 3
      case _ => x = 4
    }
    x
  }

  def test2 = {
    var x = 1
    try {
      x = 2
      try {
        x = 21
      } catch {
        case _ => x = 22
      }
      x = 23
    } catch {
      case _: NullPointerException => x = 3
      case _ => x = 4
    }
    x
  }

  def test3 = {
    var x = 1
    try {
      try{x = 2} catch { case _ => x = 4 }
    } catch {
      case _: NullPointerException => x = 3
      case _ => x = 4
    }
    x
  }

  def test4 = {
    var x = 1
    try {
      x = 2
    } catch {
      case _: NullPointerException => x = 3
      case _ => x = 4
    }
    try {
      x = 5
    } catch {
      case _: NullPointerException => x = 6
    }
    x
  }

  def test5 = {
    var x = 1
    try {
      x = 2
    } catch {
      case _: NullPointerException => try { x = 3 } catch { case f => throw f }
      case _ => x = 4; try { x = 41 } catch { case _: Exception => x = 42 }; x = 43
    }
    x
  }

  def test6: Int = {
    var x = 1
    try {
      x = 2
      (null: String).toString
    } catch {
      case e: NullPointerException =>
        throw e
      case _ =>
        x = 3
        return 1000
    } finally {
      x = 4
      println(x)
    }
    x
  }

  def test7 = {
    var x = 1
    try {
      x = 2
    } finally {
      try {
        x = 4
      } catch {
        case _ => x = 5
      }
    }
    x
  }

  def test8 = {
    var x = 1
    try {
      throw new NullPointerException
    } catch {
      case e => throw e
    }
    x
  }

  def test9 = {
    try { "" match {
      case s: String => 10
    }} catch { case _ => 20 }
  }

  var x10 = 1
  def test10: Int = {
    try { 1 }
    catch { case e if (x10 == 1) => 1 }
  }

   def test11 {
    try { () }
    catch { case e => () }
  }

  class E1 extends Exception
  class E2 extends Exception
  class E3 extends Exception

  def test12_impl(op: => Int) = try {
    op
  } catch {
    case e: E1 => 2
    case e: E2 => 3
    case e: E3 => 4
  }
  def test12 =
    test12_impl(1) +
    test12_impl(throw new E1) +
    test12_impl(throw new E2) +
    test12_impl(throw new E3)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala exceptions-nest.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.