The Artima Developer Community
Sponsored Link

Programming in Scala Forum
finally isn't executed when exception is thrown

1 reply on 1 page. Most recent reply: Mar 23, 2011 6:28 AM by George Berger

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
lab bhattacharjee

Posts: 1
Nickname: labb
Registered: Mar, 2011

finally isn't executed when exception is thrown Posted: Mar 23, 2011 4:01 AM
Reply to this message Reply
Advertisement

def tryFinally {
import java.io.Reader
var file: Reader = null
try {
import java.io.FileReader
file = new FileReader("input.txt")
} finally {
println("inside finally")
closer(file)
}
}

def closer(closeable: java.io.Closeable) {
println("inside closer")
closeable.close
}


The finally block in the above code, is not executed when java.io.FileNotFoundException is thrown while creating FileReader.

But according to 'Programming in Scala', 2nd Edition (Page 171-72), the finally should be executed.


Please let me know if I'm mistaken anywhere.


George Berger

Posts: 24
Nickname: gcb
Registered: Jul, 2007

Re: finally isn't executed when exception is thrown Posted: Mar 23, 2011 6:28 AM
Reply to this message Reply
It works okay for me:


$ scala
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def closer(closeable: java.io.Closeable) {
| println("inside closer")
| closeable.close
| }
closer: (closeable: java.io.Closeable)Unit

scala>

scala> def tryFinally {
| import java.io.Reader
| var file: Reader = null
| try {
| import java.io.FileReader
| file = new FileReader("input.txt")
| } finally {
| println("inside finally")
| closer(file)
| }
| }
tryFinally: Unit

scala>

scala> tryFinally
inside finally
inside closer
java.lang.NullPointerException
at .closer(<console>:7)
at .tryFinally(<console>:14)
at .<init>(<console>:8)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.ja va:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.app ly(Interpreter.scala:988)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.app ly(Interpreter.scala:988)
at scala.util.control.Except...
scala>

Flat View: This topic has 1 reply on 1 page
Topic: PDF corrupted for 2nd edition Previous Topic   Next Topic Topic: 2nd Edition - pg 667 - XML Pattern Matching

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use