Scala | Try-Catch Exceptions

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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program of try-catch
    // exception
      
    // Creating object
    object Arithmetic
    {
      
    // Main method
    def main(args: Array[String]) 
    {
          
        // Try clause
        try
        {
            // Dividing a number
            val result = 11/0
        }
          
        // Catch clause
        catch
        
                // Case statement
                case x: ArithmeticException => 
                
              
                // Display this if exception is found
                println("Exception: A number is not divisible by zero.")
            }
        }
    }
    }

    chevron_right

    
    

    Output:

    Exception: A number is not divisible by zero.
    

    Here, an exception is thrown as a number is not divisible by zero.

  • Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Scala program of Try-Catch
    // Exception
    import java.io.FileReader
    import java.io.FileNotFoundException
    import java.io.IOException 
      
    // Creating object
    object GfG
    {
      
    // Main method
    def main(args: Array[String]) 
    {
          
        // Try clause
        try
        {
            // Creating object for FileReader
            val t = new FileReader("input.txt")
        
          
        // Catch clause
        catch
        {
          
            // Case statement-1
            case x: FileNotFoundException =>
            {
                // Displays this if the file is
                // missing
                println("Exception: File missing")
                  
            }
          
            // Case statement-2
            case x: IOException   => 
            {
              
                // Displays this if input/output 
                // exception is found
                println("Input/output Exception")
              
            }
        }
    }
    }

    chevron_right

    
    

    Output:

    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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.