Try-Catch construct in Java and Scala have some differences :
In Java :
Try-Catch expression results in value and for each exception, we need a sperate catch block.
In Scala :
Try-Catch construct results in exception which can pattern matched and don't need separate catch block for each exception, under same catch block using case statement, we can have multiple exceptions with each implemented with next execution steps.
try catch :
Syntax :
try {//-- Code to Run//-- Code to Run}
catch {case f:java.io.FileNotFoundException => { //-- code }case n:NullPointerException => { //-- code }case e:Exception => { //-- code }}
try finally :
Syntax :
try {//-- Code to Run//-- Code to Run} finally {
//-- code will always execute
//-- with or without exception occurred}
Examples of scala Exceptions:
ArrayIndexOutOfBoundsException
ArithmeticException
java.lang.Exception
NumberFormatException
ArithmeticException
java.lang.Exception
NumberFormatException
Interview Questions on scala Exceptions:
How do you check for exceptions in Scala? < use catch >
How to raise an arithmetic exception in Scala? < throw exception >
Why try/catch in Scala is an expression?
How to raise an arithmetic exception in Scala? < throw exception >
Why try/catch in Scala is an expression?
keywords :
how to handle exceptions in scala,try catch in scala, scala exceptions, best way to handle exceptions, exception handling scala interview questions, scala tutorial, scala vs python.
sample exceptions :
sample exceptions :
java.io.FileNotFoundException: (No such file or directory)
Exception in thread "main" java.lang.NoSuchMethodError: scala.collection.mutable.Buffer$.empty()Lscala/collection/GenTraversable;
No comments:
Post a Comment