-
-
Save Araglas88/a1594b1b849dbc94bea443d3a5cbc4aa to your computer and use it in GitHub Desktop.
Scala-Try-Catch-Finally.sc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this we will see various examples of Scala Exceptional Handling. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
val a=5 | |
val b=0 | |
a/b | |
} catch{ | |
case t: Throwable => println("Divide by zero error") | |
} | |
finally { | |
println("In finally") | |
} | |
Output: | |
Divide by zero error | |
In finally |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
opensftpreadfile("location") | |
} catch{ | |
case e: FileNotFoundException => println("Could not find file.") | |
case e: IOException => println("IOException during file read") | |
} | |
finally { | |
println("In finally") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
opensftpreadfile("location") | |
} catch{ | |
case e: Throwable => println("Issue Occurred Hence Existing") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
opensftpreadfile("location") | |
} catch{ | |
case e: FileNotFoundException => functionToGenerateFile() | |
case e: IOException => sendEmailToSupport() | |
case e: Throwable => print("Some Error") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val x: String =try{ | |
val str = "GOOD" | |
str.substring(0,12) | |
} catch{ | |
case e: Throwable => "Some Error" | |
} | |
finally { | |
println("In finally") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
val str = "GOOD" | |
str.charAt(20) | |
} catch{ | |
case e: Throwable => println("String is short") | |
} | |
println("Reached this Step") | |
Output: | |
String is short | |
Reached this Step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
val str = "GOOD" | |
str.charAt(20) | |
} catch{ | |
case e: Throwable => println("String is short") | |
throw e | |
} | |
println("Reached this Step") | |
Output: | |
String is short | |
java.lang.StringIndexOutOfBoundsException: String index out of range: 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment