• Home
  • Recent Q&A
  • Java
  • Cloud
  • JavaScript
  • Python
  • SQL
  • PHP
  • HTML
  • C++
  • Data Science
  • DBMS
  • Devops
  • Hadoop
  • Machine Learning
in JAVA by
Q:
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 Apr 8, 2021 in JAVA by SakshiSharma
+1 vote
asked May 12, 2021 in JAVA by rajeshsharma
...