Previously we learned about several exceptions in Java, so how do we handle exceptions ?
We can use the try...catch statement to handle exceptions, and place possible exception operations in the try part of the try...catch statement. Once the try part throws an exception object, or calls a method that may throw an exception object, and the method If an exception object is thrown, the try part will immediately end execution and move to the corresponding catch part. Therefore, the program can put the processing after the exception occurs in the catch part.
The try...catch statement can be composed of several catches to handle the corresponding exceptions that occur.
The format of the try...catch statement is as follows:
try{//Contains statements that may cause exceptions}catch(ExceptionSubClass1e){…}catch(ExceptionSubClass2e){…}
Note : The exception class in each catch parameter is a subclass of Exceptin, indicating possible exceptions in the try part. There cannot be a parent-child relationship between these subclasses, otherwise only one catch containing the parent class parameter will be retained.