What is an Exception?
- An exception is a runtime error, which disrupts the normal execution flow of a program.
- Compile time errors could not be considered as exceptions, since they are treated as errors and not as exceptions.
- For example, Syntactical errors like missing of semicolon in the end of the statement may result in compile time errors.
- In Java, exceptions are objects which has the information like where it occured (class, method name,line number), type of the error, state of the program when the error occurred.
What exactly can be stated as exceptions?
Exceptions can be,
- Arithmetic exceptions like dividing a number by 0
- Memory exceptions. Example, accessing an index out of the size specified in an array.
- Hardware errors like problem in the interface, device malfunction; losing of network connectivity and other environmental factors could raise an exception.
- Exceptions due to the result of bad programming logic like Nullpointer exception, ClassNotFound exceptions etc.
What could be done when an exception occurs?
One of the following could be done when an exception occurs.
- Report it to the user by printing a message.
- Handle the error, if something could be done to rectify.
We can avoid entering into an inconsistent state, finding some solution to it.
The blocks in blue color represents unchecked exception and the blocks in red color represents checked exceptions. (Checked & unchecked exceptions are explained later)
Cause of an Exception:
- Due to environmental conditions (uncertain):
- Due to bad programming logic.
- Due to a wrong input (unsupported by the program) provided by the user / client.
- Errors are those which cannot be handled but just notified, whereas Exceptions are those which is caused by the programmer or the client, which can be handled in some way.
- All Errors and Exceptions comes under the parent class Throwable. The Error class and the Exception class are the subclasses of Throwable.
- Errors occur due to the failure of suitable environmental conditions which makes the program to terminate in the middle and it is out of the scope of programming logic.
- Example, Hardware malfunction due to some technical problems, connection terminated due to network problems, Linkage problem due to missing libraries, Memory goes out of stack due to the use of indefinite loops etc.
Exceptions are classified as,
- Compile time exceptions
- Runtime exceptions.
Compile time exceptions:
- Can be found in the compile time itself. These exceptions are also called as Checked exceptions.
- Checked exceptions occur due to the mistakes done by the programmer.
- Example – ClassNotFoundException occurs when the programmer has not mentioned the proper class name in the code, same way FileNotFoundException occurs when the program is trying to acces the file whose path is given wrong or the name of the file is mentioned wrong.
- Checked exceptions must be handled by the programmer either by using try, catch or by specifying the throws keyword (try, catch, throws are explained in the upcoming topic).
- The main thing which has to be remembered is that checked exceptions needs to be handled (checked).
- The compiler forces the programmer to specify ‘throws’ keyword or to handle the exception (using try, catch), if it is a checked exception.
- Are the exceptions which cannot be found while compiling the code and occurs during the execution of the program.
- Examples of runtime exceptions are ArithmeticException which occurs due to the divide by zero condition, ArrayIndexOutOfBoundException which occurs due to the access of element whose index is out of the array size declared, NullPointerException when the code is trying to access a null value when an object value is needed in its place.
- Compiler does not forces the method to handle the exception, unlike compile time exceptions.
- RunTimeException is a special subclass of Exception class, which need not have to be handled (hence called as unchecked). The reason for this is that JVM will notify the exception to the user (what made the program to terminate).
- RunTime exception occurs due to the bug present in the application code and the programmer has to fix it.
- Errors and the RunTime Exceptions are collectively called as Unchecked Exceptions.
- Before deciding the exception handling mechanism, the programmer has to think whether the client could be able to handle the exception.
- Sometimes, even a novice programmer finds the exception handling mechanism a nightmare. One has to know the best practices to be followed to implement exception handling.