Sunday 29 July 2012

Java - Exception Handling

Types of Errors
=================================
Syntax errors: arise if rules of the language are not followed.
Logical errors: Indicates that logic used for coding doesn’t produce expected output.
Runtime Errors: Occur because the program tries to perform an operation that is impossible to complete. For example divide by zero.

What is an Excpetion?
=================================
An exception is an event that usually signals an erroneous situation at run time. In java, exceptions are wrapped up as objects and can be dealt in one of three ways: ignore it, handle it where it occurs or handle it at an another place in the program.
The   exception   object   stores   information   about   the   nature   of   the   problem.   For example, due to network problem or class not found etc.

(An error  is also represented  as an  object  in  Java,  but  usually  represents  an unrecoverable situation and should not be caught.)

All exceptions in java are inherited from a class know as Throwable as shown below:





Further exceptions can be categorized into two types, Unchecked & Checked Exceptions.

Unchecked Exceptions
These exceptions does not require explicit handling. All exceptions that are subclasses of RuntimeException and Error come under unchecked exceptions.
These exceptions and errors are internal to our programs so we can get rid of them by debugging our code. For example, null pointer exception; index out of bounds exception; division by zero exception...etc.
Checked Exceptions
These must be caught or declared in a throws clause else compiler will issue an error. Exceptions other than RuntimeException come under checked exceptions.
For example - communication from file server or DB.
There are 5 keywords in java to handle exceptions. try, catch, finally, throw & throws:
  • try block: Write code inside this block which could generate errors.
  • catch block: Code inside this block is used for exception handling. When the exception is raised from try block, only than catch block would execute.
  • finally block: This block always executes whether exception occurs or not. Write clean up code here, like resources (connection with file or database) that are opened may need to be closed.
  • throw: To manually throw an exception.
  • throws: if method is not interested in handling the exception than it can throw back the exception to the caller method using throws keyword.
Now consider the below example:
Let method_B doesn’t want to handle exception by itself, so it throws the exception to the caller of method_B i.e. method_A. So method_A either have to handle the incoming exception or it can re-throw it to its caller i.e. main. Let method_A is handling the exception.



So method_A and method_B would be as:
   public class ThrowsExample { 

   public static void method_B( ) throws IOEception{ 
        FileReader fr = new FileReader (“strings.txt”); 
        BufferedReader br = new BufferedReader (fr); 
        
        //read the line form file
        String s = br.readLine(); 
        System.out.println(s); 
  }// end method_B


   // calling method_B & handling incoming exception
   public static void method_A( ) {
     try { 
       method_B(); 
     catch (IOException ioEx) { 
       ioEx.printStackTrace(); 
     } 
   }  
   public static void main (String args[ ]) { 
       ThrowsDemo.method_A(); 
   }
}

No comments:

Post a Comment