Exception handling

 Introduction to exception handling

  • An exception is a condition that is caused by a run-time error in the program.
  • When the java interpreter encounters an error such as dividing an integer by zero , it creates an exception object and throws it ( i.e. Informs us that an error has occurred).
  • If we want the program to continue with the execution of the remaining code , then we should try to catch the exception object thrown by the error condition and the display an appropriate message for taking corrective actions. This known as exception handling.
  • The exception handling mechanism has the following tasks :
  • Find the problem ( Hit the exception).
  • Inform that an error has occurred (Throw the exception).
  • Receive the error information (Catc the exception).
  • Take corrective actions (Handle the                  exception ).

Syntax of exception handling code

  • The basic concepts of exception handling are thrown an exception and catching it.
  •  This is shown in following fig.

  •  Java uses a keyword try to preface a block of code that is likely to cause an error condition and "throw"' an exception.
  • A catch block defined by the keyword catch "catches" the exception ,”thrown” by the try block and handles it appropriately.
  • The catch block is added immediately after the try block.
  • The following example illustrates the use of simple try and catch statements:

………..

………

Try

{

Statement; // generates an exception

}

Catch (Exception-type e)

{

Statement; // processes the exception

}

  • The try block can have one or more statements that could generate an exception.
  •  If anyone statement generates an exception, the remaining statements in the block are skipped and execution jumps to catch block that is placed next to the try block.
  • The catch block too can have one or more statements that are necessary to process the exception.
  • Every try statement should be followed by at least one catch statement , otherwise compilation error will occur.
  •  The catch statement works like a method definition.
  • The catch statement is passed a single parameter , which is reference to the exception object thrown by try block.
  • If the catch parameter matches with the type of exception object, then the exception is caught and statements in the catch block will be executed. Otherwise , the exception is not caught and the default exception handler will cause the execution to terminate.

Try statement

  • The try block can have one or more statements that could generate an exception.
  • If anyone statement generates an exception, the remaining statements in the block are skipped and execution jumps to catch block that is placed next to the try block.

Catch statement

  •  The catch statement works like a method definition.
  • The catch statement is passed a single parameter , which is reference to the exception object thrown by try block.
  •  If the catch parameter matches with the type of exception object, then the exception is caught and statements in the catch block will be executed. Otherwise , the exception is not caught and the default exception handler will cause the execution to terminate.

Finally statement

  •  Java supports another statement known as flnally statement that can be used to handle an exception that is not caught by any of the previous catch statements.
  •  Finally block can be used to handle any exception generated within a try block.
  •  It may be added immediately after the try block or after the last catch block shown as

follows:

Try

{

…………

………….

}

Finally

{

…………

………….

}

Or another way….

Try

{

…………

………….

}

Catch(…….)

{

…………

}

Catch(…….)

{

…………

}

Finally

{

…………

………….

}

  • When a finally block is defined, this is guaranteed to execute, regardless of whether or not in exception is thrown it means statements written in finally block are executed whether the exception is caught or not.
  •  As a result, we can use it to perform certain house-keeping operations such as closing files and releasing system resources.

No comments:

Post a Comment