The Try-Catch construct is different in Scala than in Java, Try-Catch in Scala is an expression. the Scala make use of pattern matching in the catch clause. Suppose, we have to implement a series of code which can throw an exception and if we want to control that exception then we should utilize the Try-Catch segment as it permits us to try-catch each and every type of exception in only one block, we need to write a series of case statements in catch as Scala uses matching in order to analyze and handle the exceptions.
-
Example :
// Scala program of try-catch// exception// Creating objectobjectArithmetic{// Main methoddefmain(args:Array[String]){// Try clausetry{// Dividing a numbervalresult=11/0}// Catch clausecatch{// Case statementcasex:ArithmeticException=>{// Display this if exception is foundprintln("Exception: A number is not divisible by zero.")}}}}chevron_rightfilter_noneOutput:Exception: A number is not divisible by zero.
Here, an exception is thrown as a number is not divisible by zero.
-
Example :
// Scala program of Try-Catch// Exceptionimportjava.io.FileReaderimportjava.io.FileNotFoundExceptionimportjava.io.IOException// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// Try clausetry{// Creating object for FileReadervalt=newFileReader("input.txt")}// Catch clausecatch{// Case statement-1casex:FileNotFoundException=>{// Displays this if the file is// missingprintln("Exception: File missing")}// Case statement-2casex:IOException=>{// Displays this if input/output// exception is foundprintln("Input/output Exception")}}}}chevron_rightfilter_noneOutput:Exception: File missing
Here, the try block is executed first and if any exception is thrown then each of the cases of the catch clause is checked and the one which matches the exception thrown is returned as output.
Recommended Posts:
- Scala | Finally Exceptions
- How to resolve unhandled exceptions in Node.js ?
- Exceptions Vs Errors in PHP
- Raising Exceptions in Ruby
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

