Chapter 10 |
Catch exceptions
An exception can be handled using a combination of the try
and catch
keywords. A try/catch block is placed around the code that might generate an exception. The code within the try/catch block is referred to as protected code, and the syntax for using the try/catch blocks looks as follows:
try { // Protected code } catch (ExceptionName e1) { // Catch block }
The code which might cause exceptions is placed in the try
block. When an exception occurs, the method execution is halted at the line which causes the exception, and the exception itself is passed to the corresponding catch
block.
A catch statement involves declaring a type of the exception we are trying to catch. If an exception occurs in the protected code, the catch
block (or blocks) that follows the try
is checked. If the type of the exception that occurs is listed in the catch
block, the exception is passed to that catch
block.
A try
block can be followed by multiple catch
blocks. The syntax for multiple catch
blocks looks as follows:
try { // Protected code } catch (ExceptionType1 e1) { // Catch block } catch (ExceptionType2 e2) { // Catch block } catch (ExceptionType3 e3) { // Catch block }
This example statement demonstrates three catch
blocks, but we can have any number of them after a single try
. If one of the statements inside the try
block throws an exception, Java skips the remaining statements in the try
block and starts the process of finding the code to handle the exception. The code that handles the exception is called the exception handler; it is found by propagating the exception backward through a chain of method calls, starting from the current method. Each catch
block is examined in turn, from first to last, to see whether the type of the exception object is an instance of the exception class in the catch
block. If so, the exception object is assigned to the variable declared, and the code in the catch
block is executed. If no handler is found, Java exits this method, passes the exception to the method that invoked the method, and continues the same process to find a handler. If no handler is found in the chain of methods being invoked, the program terminates and prints an error message on the console. The process of finding a handler is called catching an exception.
If no exceptions arise during the execution of the try
block, the catch
blocks are skipped.
In the following example, the user is prompted for a file name and the program tries to open the file. If the file opening causes the FileNotFoundException
exception, the catch
block catches the exception and asks the user for the file name once again:
import java.io.*; import java.util.*; public class FileReader { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter a file name: "); String fileName = s.nextLine(); Scanner fileReader = null; while (fileReader == null) { try { fileReader = new Scanner(new File(fileName), "UTF-8"); // may cause an exception System.out.println("The fileReader is ready"); // if the previous statement causes an exception, this line will not be executed } catch (FileNotFoundException e) { System.out.println("No such file. Enter a new file name: "); fileName = s.nextLine(); } } // here we can add statements to process the file s.close(); fileReader.close(); } }
Pay attention that the signature of the catch
block has to contain a formal parameter which defines the exception type (e.g. in the example above, the formal parameter is e
and its data type is java.io.FileNotFoundException
). If the exceptions is not associated with the data type of the formal parameter (or its subclass's type) of the catch
block, the latter will ignore the exception.
Chapter 10 |