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") }
?
Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System. exit() it will execute always.
@barth When there's no catch block the exception thrown in finally will be executed before any exception in the try block. So If there are two exceptions one in try and one in finally the only exception that will be thrown is the one in finally .
The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error. Both catch and finally are optional, but you must use one of them.
Syntax. The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by a catch block or finally block.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With