tags:

views:

561

answers:

2

Unlike Java, Scala lets you do a bare "try", without either a catch or finally clause:

scala> try { println("Foo") }
Foo

Does this actually have any meaning beyond,

{ println("Foo") }

?

+1  A: 

I don't think so. I think Scala permits a try without a catch or finally because there's no reason to forbid it.

skaffman
Well, presuming that there's never any actual reason to use a bare "try", a reason to forbid it would be to catch programmer mistakes and reduce potential confusion. But certainly, it's not a big deal either way.
Matt R
+10  A: 

Scala's exception handling works by passing any exceptions to an anonymous catch function. The catch function works by pattern matching the caught exception and, if it doesn't match it will pass the exception up.

The catch function is optional, if it's omitted then the exception is passed straight up. So essentially

try { exceptionThrowingFunction() }

is the same as

exceptionThrowingFunction()

See chapter 6.22 of the language spec pdf for more information.

Dan Midwood