Wednesday, February 15, 2012

Introducing exception handling

In programming terms, an exception is an event that stops the normal execution of a program.

For example, a program might try to access an element outside the bounds of an array, or some file input and output operation might fail.

You need to ensure that your Java programs can deal with exceptions without simply crashing.
When an exception occurs at run time, the method in which it occurs creates an exception object. Execution is aborted unless the exception is handled somewhere along the call stack.

The method can throw the exception back to the calling method, which may be better able to handle it. The exception object includes information about the exception, such as the method in which it occurred and the cause of the exception.
The calling method can, in turn, throw the exception back to its caller. It's up to the developer to provide exception-handling code within the appropriate methods.
If no method handles the exception, the program terminates when the exception object reaches the top of the visible call stack - the main method.
When an exception occurs, you can prevent it being passed up the call stack – and potentially terminating the application – by providing handling code for the particular exception type, enabling the application to continue execution along a different path.

You can direct an application to use an exception handler in the method in which the exception occurs, or in one of that method's calling methods.
Once the exception is handled, the program continues to run, although this depends on the specific implementation.

3 comments:

Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on
SEO Company in Chennai

Post a Comment