Answer:
Throw the exception to the method that called this method, or handle the exception in a catch block.
Explanation:
First let us understand what an exception is. A program is a set of instructions and it is executed in a flow which is called flow of program which an exception can interrupt. This exception is basically an unexpected event. Exception can be checked (which is compile time exception) or unchecked (run time exception). When the exception object is created to indicate the type of error occurred which interrupted the program flow this exception is thrown which indicates that an unexpected event or error has occurred in a program.
So when a exception is thrown one of two ways are used to deal with it
- Handle the exception in a catch block,
- Throw the exception to the method that called this method. It usually has a throw clause which defines what exceptions a method can handle and what not.
Exceptions are handles using try catch block. In this block there are two keywords which are used i.e. try and the other one is catch.
try basically defines a block of code. This block of code is executed and is checked for exceptions.
Now if the exception occurs, catch statement is used to handle this exception. So the catch part is executed when an exception occurs. However if catch statement fails to process this exception then this is thrown to the caller method.
A very common example is of a checked exception FileNotFoundException.
This error occurs when a program wants to read a file. So we get a compiler time exception to handle for situations in which the file does not exist in the location.
So this checked exception is handles either by the catch block or by throwing this exception to the method that called this method.
In catch block we can determine how to handle if the file is not found. If catch block is unable to handle this exception then we will use throw clause. So whenever this exception occurs the methods can throw the IOException and FileNotFoundException is basically a subclass of this. For example in JAVA, java.io.IOException is used for handling such exceptions.