An exception is an event, which occurs during the execution of a program, that discontinues the normal flow of program and prevents the program from executing further.
An errorindicates that a non-recoverable condition that has occurred that cannot be caught and taken care of. Error is a sub class of Throwable class.
There are two types exceptions in Java:-
i. Checked Exception
ii. Unchecked Exception
An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. Exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist.
There are two ways to handle exceptions
i. Wrapping the desired code in a try block followed by a catch block to catch the exceptions.
ii. List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.
By putting it within finally block.
The Exception class has two main sub classes: IOException class and RuntimeException Class.
A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.