You are here: Home / Topics / Try and catch example in Java

Try and catch example in Java

Filed under: Java on 2023-09-19 06:44:48

//  Simple try and catch example for exception handling

class  SimpleTryCatchExample 
{
public static void main(String args[ ]) 
{
 int d, a;
 try 
 { 
  d = 0;
  a = 10 / d;
  System.out.println("This will not be printed.");
 } 
 catch (ArithmeticException e) 
 { 
  System.out.println("Division by zero. Arithmetic Exception");
 }
 System.out.println("After catch statement.");
}
}


Output:

Division by zero. Arithmetic Exception
After catch statement.


About Author:
M
Mr. Dubey     View Profile
Founder of MCQ Buddy. I just like to help others. This portal helps students in getting study material free. Share your stuff here so that others can get benefitted.