Exception Handling in Java

Introduction 

 
Exceptions are runtime anomalies or unusual conditions that a program may encounter during executing. Anomalies might include conditions such as division by zero, access to an array outside of bounds, or running out of memory or disk space.
 
Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as “out of range index” and “overflow” belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program are called asynchronous exceptions.
 
The purpose of exception handling is to provide a way to detect and report an “exceptional circumstance” so that the appropriate action can be taken.
 

Java Exception Handling Keywords

 
There are 5 keywords used in Java exception handling.
  • Try
  • Catch
  • Finally
  • Throw
  • Throws

Advantage of Exception Handling

 
The core advantage of exception handling is to maintain the normal flow of the application. Exceptions normally disrupt the normal flow of the application. That is why we use exception handling. Let's take a scenario:
 
Exception Handling
 
Assume there are 3 statements in your program and there occurs an exception in the first at the statement, the rest of the code will not be executed. In other words, the second and third statements will not run. If we perform exception handling then the rest of the exception will be executed. That is why we use exception handling in Java.
 
Example
 
In this program, we use to try and catch.
  1. class demo  
  2. {  
  3.     public static void main(String arg[])  
  4.     {  
  5.         try  
  6.         {  
  7.             System.out.println(5/0);  
  8.         }  
  9.         catch(Exception e)  
  10.         {  
  11.             System.out.println("U can't devide by zero");  
  12.         }  
  13.         System.out.println("Hello india");  
  14.     }  
  15. }  
Step 1
 
Name it "excep.java" and save the file in any location. I saved it at "c:/kiran/program".
 
Step 2
 
Set the path, without the path setting our Java file is not compiled.
 
path
 
Step 3
 
Now use the following command for checking to compile the Java file.
 
javac excep.java.
 
rogram compiled
 
My Java program compiled successfully.
 
Step 4
 
Write the following code in the command prompt. Press Enter and see the output.
 
Java demo
  1. // demo is a class name which is written in my "excep.java" file.   
  2. //Output  
java demo
 
Step 5
 
If we use (5/3) then the output is:
 
output is
 
Step 6
 
Now, use the try, catch and finally.
 
Code
  1. class demo  
  2. {  
  3.     public static void main(String arg[])  
  4.     {  
  5.           
  6.         try  
  7.         {  
  8.             System.out.println(5/4);  
  9.         }  
  10.         catch(Exception e)  
  11.         {  
  12.             System.out.println("U Can't Divided by Zero");  
  13.         }  
  14.         finally  
  15.         {  
  16.             System.out.println("Hello india");    
  17.         }  
  18.           
  19.     }  
  20. }  
Output
 
Output
 
Step 7
 
If we use (5/0) the output is:
 
put
 
In other words, the finally block always executed whether an exception occurs or not.
  
Step 8
 
Now, we use try, catch and throw.
 
Code
  1. class MyExcep extends Exception  
  2. {  
  3.     String msg()  
  4.         {  
  5.         return "U are not valid";  
  6.         }  
  7. }  
  8. class CoreExcep  
  9. {  
  10.     void Checkage(int age) throws Exception  
  11.         {  
  12.     if(age<=18)  
  13.     {    
  14.         throw new MyExcep();  
  15.     }     
  16.     else  
  17.     {  
  18.             System.out.println("U are valid");  
  19.     }  
  20. }  
  21.     public static void main(String arg[]) throws Exception  
  22.     {  
  23.         CoreExcep ce=new CoreExcep();  
  24.         try{  
  25.         ce.Checkage(20);  
  26.         }  
  27.         catch (MyExcep e){  
  28.         System.out.println(e.msg());}  
  29.         }  
  30. }  
Throws
 
Step 9
 
If we use 17 instead of 20.
 
Code
  1. class MyExcep extends Exception  
  2. {  
  3.     String msg()  
  4.         {  
  5.         return "U are not valid";  
  6.         }  
  7. }  
  8. class CoreExcep  
  9. {  
  10.     void Checkage(int age) throws Exception  
  11.         {  
  12.     if(age<=18)  
  13.     {    
  14.         throw new MyExcep();  
  15.     }     
  16.     else  
  17.     {  
  18.             System.out.println("U are valid");  
  19.     }  
  20. }  
  21.     public static void main(String arg[]) throws Exception  
  22.     {  
  23.         CoreExcep ce=new CoreExcep();  
  24.         try{  
  25.         ce.Checkage(17);  
  26.         }  
  27.         catch (MyExcep e){  
  28.         System.out.println(e.msg());}  
  29.         }  
  30. }  
See the output
 
See the output
 


Similar Articles