dave
dave

Reputation: 12806

How does this try finally block work?

I have a function where I want do this:

def someThing():Int = {
 val thingy:Thing = new Thing()
 try{
  thingy.getIntThingy()
 }finally{
  thingy.cleanUp()
 }
}

getIntThingy() returns an Int. There are temporary tables in thingy that are created on initialization that needs to be cleaned up (side effects). Will this code work or should I refactor?

Upvotes: 1

Views: 160

Answers (1)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297295

Well, you need to get the value from the try block, but it works. For example:

scala> val x = try { 1 } finally { println("yay") }
yay
x: Int = 1

Upvotes: 4

Related Questions