Monday, June 20, 2016

Exception Handling Example Java

Exception Handling

When there is an exception ,the user data may be corrupted.This should be tackled by the programmers be carefully designing the program.He/She should watch this 3 steps:

  1. The programmer should observe in his program where there may be a possibility of exception.Such statements should be written inside a try block.                                                               try{statement;}                                                                                                                                                                                                                                                                                  The greatness of try block is that even if some exception arises inside it,the program will not be terminated.
  2. The programmer should write the catch block where he should display the exception details to the user.                                                                                                                                                     catch(ExceptionClass ref){statements;}
  3. Finally,the programmer should perform clean up operations like closing the files and termination of threads.                                                                                                                     finally{statements;} 
Program:


/**
 * 
 */
package com.collectionpack;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class EceptionHandlingApp 
{
/**
* @param args
*/
public static void main(String[] args)
{
try
{
System.out.println("open file");
 int n=args.length;
 System.out.println("n= + " +n);
 int a =45/n;
 System.out.println("a "+a);
 System.out.println("Close File");
}
catch (Exception e)
{
e.printStackTrace();
}

}

}

Output: 

open file
n= + 0
java.lang.ArithmeticException: / by zero
at com.collectionpack.EceptionHandlingApp.main(EceptionHandlingApp.java:22)

Handling Multiple Exceptions:

Most of the times there is possibility of more than one exception present in the program.In this case programmer should use more than one catch blocks.

Program:

/**
 * 
 */
package com.collectionpack;

/**
 * @author Abhinaw.Tripathi
 *
 */
public class MultipleExceptionHandlingApp {

/**
* @param args
*/
public static void main(String[] args)
{
 try
 {
System.out.println("open file");
 int n=args.length;
 System.out.println("n= + " +n);
 int a =45/n;
 System.out.println("a "+a);
 int b[]={10,20,30};
 b[50]=100;
 
 catch (ArithmeticException e)
 {
e.printStackTrace();
 }
 catch (ArrayIndexOutOfBoundsException e)
 {
 System.out.println("ArrayIndexOutOfBoundsException ");
e.printStackTrace();
 }
 
 finally
 {
 System.out.println("Close File");
 }
 
}

}

Output:

open file
n= + 0
Close File
java.lang.ArithmeticException: / by zero
at com.collectionpack.MultipleExceptionHandlingApp.main(MultipleExceptionHandlingApp.java:22)


This means, even if there is scope for multiple exceptions,only one exception at a time will occur.


Bullet Points in Exception Handling:
  • An exception can be handled using try,catch and finally blocks.
  • It is possible to handle multiple exceptions using multiple catch blocks.
  • Even though there is multiple exceptions,only one exception at a time will occur.
  • We can not write catch without try block.
  • It is not possible to insert some statement within try{} and catch() blocks.
  • Nested try can be possible.

No comments: