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

Scala example source code file (finally.scala)

This example Scala source code file (finally.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

a, app, app, exception, exception, here, here, in, int, running, runtimeexception, should, test, test

The Scala finally.scala source code


object Test extends App {


  // test that finally is not covered by any exception handlers.
  def throwCatchFinally {
    try {
      bar
    } catch {
      case e => println(e)
    }
  }
  
  // test that finally is not covered by any exception handlers.
  def bar {
    try {
      println("hi")
    }
    catch {
      case e => println("SHOULD NOT GET HERE")
    }
    finally {
      println("In Finally")
      throw new RuntimeException("ouch")
    }
  }

  // return in catch (finally is executed)
  def retCatch {      
    try {
      throw new Exception
    } catch {
      case e =>
        println(e);
        return
    } finally println("in finally")
  }

  // throw in catch (finally is executed, exception propagated)
  def throwCatch {      
    try {
      throw new Exception
    } catch {
      case e =>
        println(e);
        throw e
    } finally println("in finally")
  }

  // return inside body (finally is executed)
  def retBody {      
    try {
      return
    } catch {
      case e =>
        println(e);
        throw e
    } finally println("in finally")
  }

  // throw inside body (finally and catch are executed)
  def throwBody {
    try {
      throw new Exception
    } catch {
      case e =>
        println(e);
    } finally println("in finally")
  }

  // return inside finally (each finally is executed once)
  def retFinally {
    try {
      try println("body")
      finally {
        println("in finally 1")
        return
      } 
    } finally println("in finally 2")
  }


  // throw inside finally (finally is executed once, exception is propagated)
  def throwFinally {
    try {
      try println("body")
      finally {
        println("in finally")
        throw new Exception
      }
    } catch {
      case e => println(e)
    }
  }

  // nested finallies with return value
  def nestedFinalies: Int = 
    try {
      try {
        return 10
      } finally {
        try { () } catch { case _ => () }
        println("in finally 1")
      }
    } finally {
      println("in finally 2")
    }

  def test[A](m: => A, name: String) {
    println("Running %s".format(name))
    try {
      m
    } catch {
      case e => println("COUGHT: " + e)
    }
    println("-" * 40)
  }

  test(throwCatchFinally, "throwCatchFinally")
  test(retCatch, "retCatch")
  test(throwCatch, "throwCatch")
  test(retBody, "retBody")
  test(throwBody, "throwBody")
  test(retFinally, "retFinally")
  test(throwFinally, "throwFinally")
  test(nestedFinalies, "nestedFinalies")
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.