Scala Exceptions + try catch finally
Jakob Jenkov |
Scala has an exception mechanism similar to Java's.
Exception handling is a rather large topic to cover in full detail. Therefore, I will be writing more about Scala exception handling in a different, more specialized trail. In this text I will just explain the exception handling mechanisms briefly.
Throw Exception
A Scala method can throw an exception instead of returning normally, in case an error occurs. Here is an example which should look familiar to Java developers:
//do something //do something more throw new IllegalArgumentException("arg 1 was wrong..."); //nothing is executed after the throw.
When an exception is thrown the normal thread of execution is interrupted, and the exception
is propagated up the call stack until a catch
clause catches it.
Try Catch
If you execute a sequence of code that might throw an exception, and you would like to handle that exception, you use the try-catch block. Here is an example:
try{ throwsException(); println("this line is never executed"); } catch { case e: Exception => println("exception caught: " + e); } //A METHOD THAT THROWS EXCEPTION def throwsException() { throw new IllegalStateException("Exception thrown"); }
When an exception is thrown from inside the throwsException()
method, the execution
is interrupted and the execution jumps to the catch clause
surrounding the code that
threw the exception.
In the example above, when the throwsException()
method is called,
and it throws an exception, the statement below the throwsException()
method call is
never executed. Instead the execution jumps down to the catch
clause.
The catch
clause looks a bit different from a Java catch clause. Rather than declare
the exception to catch in parantheses after the catch keyword, Scala requires that you write a
list of case
statements.
In the example above, only one exception is caught, Exception
which is the superclass of most exceptions.
Hence, most exceptions are caught using that catch clause. If you wanted to be more specific in what exceptions
to catch, provide multiple case statements. For instance:
catch { case e: IllegalArgumentException => println("illegal arg. exception"); case e: IllegalStateException => println("illegal state exception"); case e: IOException => println("IO exception"); }
Finally
The finally
clause can contain code that you need to be executed, no matter if an
exception is thrown or not. Here is an example:
try { throwsException(); } finally { println("this code is always executed"); }
A finally
clause is appended to a try
block. You can have a catch
clause inthere too, if you like. The finally
clause is still always executed.
The finally
clause normally contains code that must be executed no matter whether an exception
is thrown or not. For instance, say you were processing a stream of data. You would want to close that stream
of data, regardless of whether your processing succeeded, or resulted in an exception being thrown.
Here is a full example, with both a try, catch and finally clause:
try { throwsException(); } catch { case e: IllegalArgumentException => println("illegal arg. exception"); case e: IllegalStateException => println("illegal state exception"); case e: IOException => println("IO exception"); } finally { println("this code is always executed"); }
Tweet | |
Jakob Jenkov |