In java Exception handling we are having 5 key words :
1) try
2) catch
3) finally
4) throw
5) throws
Before try and catch : The program goes to abnormal termination .
class Test 
{ 
 public static void main(String[] args) 
 { 
System.out.println("hi"); 
System.out.println(10/0); 
System.out.println("bye"); 
} 
}
Output :
hi Exception in Thread main:java.lang.ArithmeticException: / by zero
using try catch:-
1) If we are taking try-catch the program goes to normal termination. Because the risky code we are taking inside the try block and handling code we are taking inside the catch block.
2) If the exception is raised in the try block the corresponding catch block is executed.
3) If the corresponding catch block is not there program goes to abnormal termination.
class Test 
{ 
public static void main(String[] args) 
{ 
System.out.println("hi"); 
try 
{ 
System.out.println(10/0); 
} 
catch (ArithmeticException e) 
{ 
System.out.println("exception is "+e); 
} 
System.out.println("bye"); 
} 
}
Output :
Output:- hi exception is: java.lang.ArithmeticException: / by zero bye
If the catch block is not matched the program is terminated abnormally.
class Test 
{ 
public static void main(String[] args) 
{ 
System.out.println("program starts"); 
try 
{ 
int[] a={10,20,30}; 
System.out.println(a[0]); 
System.out.println(a[1]); 
System.out.println(a[2]); 
System.out.println(a[3]); 
} 
catch(ArithmeticException ae) 
{ 
System.out.println("we are getting exception"); 
} 
System.out.println("rest of the code"); 
} 
}
Output :
program starts 10 20 30 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at com.freetmielearn.z.main(z.java:14)
if there is no exception in try block the catch blocks won't be executed
Class Test 
{ 
public static void main(String[] args) 
{ 
System.out.println("program starts"); 
try 
{ 
System.out.println("hi"); 
System.out.println("bye"); 
} 
catch(ArithmeticException ae) 
{ 
System.out.println("we are getting exception"); 
} 
System.out.println("rest of the code"); 
} 
}
Output :
hi,bye
In between try and catch independent statements are not allowed. If we are providing independent statements the compiler will raise compilation error.
class Test 
{ 
public static void main(String[] args) 
{ 
System.out.println("program starts"); 
try 
{ 
int a=10/0; 
} 
System.out.println(in between try and catch); 
catch(ArithmeticException e) 
{ 
int a=10/5; 
System.out.println(a); 
} 
System.out.println("rest of the code is avilable"); 
} 
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error, insert "Finally" to complete TryStatement Syntax error, insert ")" to complete MethodInvocation Syntax error, insert ";" to complete Statement at com.freetmielearn.z.main(z.java:11)
The exception raised in catch block it is always abnormal termination.
class Test 
{ 
public static void main(String[] args) 
{ 
System.out.println("program starts"); 
try 
{ 
int a=10/0; 
} 
catch(ArithmeticException e) 
{ 
int a=10/0; 
System.out.println(a); 
} 
System.out.println("rest of the code is avilable"); 
} 
}
Output :
output:exception