0 votes
in JAVA by
Is it necessary that each try block must be followed by a catch block?

1 Answer

0 votes
by
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. So whatever exceptions are likely to be thrown should be declared in the throws clause of the method. Consider the following example.

public class Main{  

     public static void main(String []args){  

        try{  

            int a = 1;   

            System.out.println(a/0);  

        }  

        finally  

        {  

            System.out.println("rest of the code...");  

        }  

     }  

}  

      

Output:

Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...

Related questions

0 votes
asked May 3, 2021 in JAVA by Robindeniel
0 votes
asked Oct 18, 2019 in C Sharp by Robin
...