Monday, 17 June 2013

Exception Handling

http://howtodoinjava.com/wp-content/uploads/ExceptionHierarchyJava.png
Exception Handling

Exception handling:

In programming languages,some time our program compile successfullybut failed to execute program, such type of error interrupt program and runtime environment of language automatically throw appropriate exception. In such a case rest of the programdoesnot execute. If we want to execute rest of the program it gives an error message,then they hav to handle exception which is thrown by runtime environmen of the language, and the technique used to handle runtime error is called exception handling.

There are two types of exception classes:
1 Built in exception classes.
2 User defined exception classes.

There are 5 reserved keyword which help to handle runtime error:
1 try
2 catch
3 throw
4 throws
5 finally

1 try :  It is used where chances of runtime error are there. Runtime environment automatically detect type of error and throw object of appropriate exception class.
        Syntax:        
           try
        {
       The code that may generate an exception is placed in this block.
         }

2 catch : A exception which is thrown by try block must be brought either in catch block or finally block.

        Syntax:            
         catch(Exception_name objectname)
     {
              Statements/ error to display to user when exception occour.
}


3 throw: When ever a exception occour throw block throws the exception to the catch block. Each exception that occour must be caught, the type of exception generated is catched by the catch block of that particluar exception, catch block is declared below the try block.

           Syntax:    
                   try
              {
              The code that may generate an exception is placed in this block.
                }
                  catch(Exception_name objectname)
             {
                  Statements/ error to display to user when exception occour.
                }  

4 throws: throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method


5 finally : execute in every situation even the exception occour or not.finally create a block of code that will be executed afetr a try catch block completed and before the code following try catch block. If an exception is thrown finally block will execute eve if no catch satement matches exception.

full details in json
Proudly Powered by Blogger.
back to top