Explain about exception handling with multiple exception?

Answer:
Exception handling in Java is done through a try-catch-finally block. The "try" block of code is where you put the code which may throw an exception. The "catch" block of code is where you put the code which will execute after an exception is thrown in the try block. This is often used to display an error message, or to mitigate problems caused by the exception. The "finally" block is where you put code that you want to execute after the try and catch blocks have been processed.

// example code for catching exception while reading from a file
try {

// this line of code can throw a FileNotFoundException
FileReader in = new FileReader("myfile.txt");

// this line of code can throw an IOException
in.read();

} catch(FileNotFoundException ex) { // catch first exception type

System.err.println("Cannot find myfile.txt!");

} catch(IOException ex) { //catch second exception type

System.err.println("Cannot read from myfile.txt!");

} finally { // no matter what we want to close the file, so do it here

// however, this line can also cause an exception, so we need to catch that, too
try {
in.close();
catch(IOException ex) {
// not much we can do about an exception that occurs here
}


}

First answer by Moobler. Last edit by Sandipsingh48. Contributor trust: 0 [recommend contributor recommended]. Question popularity: 1 [recommend question].