What is Exception Handling?

An exception can be defined as “anything which interrupts the normal program flow”. The program processing will be stopped immediately, whenever there is an exception. Exception occurred during run-time are called as runtime exceptions and exception occurred during compile time are called as compile time exceptions.

Exception and Errors- Difference

Errors are complicated and crucial problems present in the machine or JVM. Errors are the problems which were not expected by the developer. Exceptions are the conditions that present in the code which can be rectified by the developer.

Why Exception Handling is Essential?

If the developer did not handle the raised exception, an unfriendly user message with compiler error is printed out as an output.

Exception Handling Advantages

When exception is handled by the developer, it will allow the program to return to its normal flow.

When calling method encounters an error, an exception will be thrown. So it allows the developer to handle the error in the calling the method.

Using try-catch blocks, separate block codes can be differentiated from block codes which contains error.

Types of Exception Handling

There are many types of exception handling but exception handling such as Arithmetic Exception, NumberFormatException, ArrayIndexOutOfBounds Exception, StringIndexOutOfBound Exception and NullPointer Exception are the commonly used Exception handling.

Arithmetic Exception

Class: Java.Lang.ArithmeticException

The class mentioned above is a default class for arithmetic exception. The above mentioned class is stored in java.lang.package. Arithmetic exception will occur whenever an integer is divided by zero.

Output for the above program is NullPointException. In the above program length() is defined as a function. The function should be used on an object but the object present in this program is null. So a NullPointException is thrown.