Exception handling keywords in java

Exception handling keywords in java


There are 5 keywords to handle the exceptions in Java
=> try
=> catch
=> finally
=> throw
=> throws


The try block is used to place the code that might throw any exception. It should be used in the method and try block should be followed by either catch or finally block.

try block followed by the catch block


try{
       //write risky code that might throw exception
}
catch(Exception_Class ref){
       //alternative solution
}

try block followed by finally block


try{
       //write risky code that might throw exception
}
finally{
       //write cleanup activities
}


The catch block is used to write the code for handling exceptions. It should be written after try block, we can use multiple catch blocks with a single try block. That multiple catch blocks order should be child exception to parent exception class.

try{
//place the risky code
}
catch(ArithmeticException ae){
//child exception class
}
catch(Exception e){
//parent exception class
}

The finally block is used to write the code for cleanup activities, such as close the database connection. finally block is always executed whether exception throws or not.


Exception handling without try-catch


Let’s check the problem if we don't use a try-catch block to handle the exceptions.


package com.exceptions;
public class Test {
       public static void main(String[] args) {
              System.out.println("statement one");           
              int i=10/0; // throw exception
              System.out.println("iam in try : "+i);         
              System.out.println("statement two");
       }
}


Output:



statement one
Exception in thread "main" java.lang.ArithmeticException: / by zero
       at com.exceptions.Test.main(Test.java:5)


In the above example, statement one executed normally but the remaining code won’t executed because JVM throws an ArithmeticException at line 5th i.e. int i=10/0; and terminate the program abnormally without executing remaining code.


This execution process is abnormal termination.

Exception handling with try-catch



package com.exceptions;
public class Test {
       public static void main(String[] args) {
              System.out.println("statement one");           
              try {
                     int i=10/0; // throw exception
                     System.out.println("iam in try : "+i);
              } catch (ArithmeticException e) {
                     System.out.println("don't divide by zero "+e );
              }            
              System.out.println("statement two");
       }
}


Output:



statement one
don't divide by zero java.lang.ArithmeticException: / by zero
statement two


The above program executed normally because of handled exception by using try catch blocks. Place the risky code that might throw an exception in the try block.

If any exception occurred in try block then alternatively execute the catch block to continue the rest of the program normally as shown in the above program.


This execution process is normal termination.


Few cases we must know using try-catch



package com.exceptions;
public class Test {
       public static void main(String[] args) {
              try {
                     System.out.println("statement one");           
                     System.out.println("statement two : "+10/0);          
                     System.out.println("statement three");
              } catch (ArithmeticException e) {
                     System.out.println("don't divide by zero "+e);
              }
       }
}


If you notice the above program, we wrote all the code in the try block so this is the bad programming because if JVM throws an exception at statement two then remaining program won’t execute whether we handled the exception or not.

Once JVM throws an exception in try block then control will go to catch block and execute the program but not go back for executing the remaining code in the try block.


The above program will give the result:


statement one
don't divide by zero java.lang.ArithmeticException: / by zero


Make sure that write the code in try block which might throw an exception but not all the code.

Look at below program for good practice and follow the cases J


package com.exceptions;
public class Test {
       public static void main(String[] args) {       
              System.out.println("statement one");
              try {
                     //assume exception might throw
                     System.out.println("statement two");                  
              }
              catch (Exception e) {
                     System.out.println("catch block "+e);
              }
              System.out.println("statement three");
       }
}


Case-1: No Exception


If the program doesn’t throw an exception then it will give the output as


statement one
statement two
statement three


with normal termination.

Case-2: Throw an Exception at statement two and catch block matched


If the program throws an exception at statement two and corresponding catch block matched then it will give result as


statement one
catch block with exception message
statement three


with normal termination.

Case-3: Throw an Exception at statement two and catch block not matched


If the exception throws at statement two and corresponding catch block not matched then it will give the output as


statement one
Exception in thread "main" exception class: information
       at com.exceptions.Test.main(Test.java:7)


with abnormal termination.

Case-4: Exception at catch block



If exception throws at statement two then control will go to the corresponding catch block and catch block also throws an exception then JVM throw an exception and terminate the abnormally same as case-3.


No comments:

Post a Comment