Welcome to Home.

Tuesday, June 23, 2015

Exceptions in Programming Language


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 an exception object is not caught and handled properly, the interpreter will display an error message and terminate the program. 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 then display an appropriate message for taking corrective actions. This task is known as exception handling.
The purpose of exception handling mechanism is to provide a means to detect and report an “exceptional circumstance “so that appropriate action can be taken.  The mechanism suggests incorporation of a separate error handling code that performs the following tasks:
1)      Find the problem( Hit the exception)
2)      Inform that an error has occurred (Throw the exception)
3)      Receive the error information(Catch the exception)
4)      Take corrective actions (Handle the exception)
The error handling code basically consists of two segments, one to detect errors and to throw exceptions and the other to catch the exceptions and to take appropriate actions.
When writing programs, we must always be on the lookout for places in the program where an exception could be generated. Some common exceptions that we must watch are listed in Table.
Exception type
                         Cause of Exception
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException

FileNotFoundException
IOException

NullPointerException
NumberFormatException
OutOfMemoryException


Caused by math errors such as division by zero.
Caused by bad array indexes.
Caused when a program tries to store the wrong type of data in an array.
Caused by an attempt to access a nonexistence file.
Caused by general I/O Failures, such as inability to read from a file.
Caused by referencing a null object.
Caused when a conversion between strings and number fail
Caused when there’s not enough memory to allocate a new object.











No comments:

Post a Comment