The exception handler, which consists of a try, catch, finally, and subsequent block. Finally is not a must.
The parentheses of a catch have a parameter that represents the type of exception you want to catch. Catch captures the corresponding type and its derived class. The block following the try contains the action to be taken against the exception type. A try-monitored block may throw more than one type of exception, so an exception handler can have multiple catch modules. Finally, the program block is the program that executes regardless of whether an exception occurs.
ImportJava.io.BufferedReader;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException; Public classtestioexception { Public Static voidMain (string[] args)throwsIOException {bufferedreader br=NULL; Try{br=NewBufferedReader (NewFileReader ("file.txt")); } Catch(FileNotFoundException E1) {e1.printstacktrace (); } Try{StringBuilder SB=NewStringBuilder (); String Line=Br.readline (); while(line!=NULL) {sb.append (line); Sb.append ("\ n"); Line=Br.readline (); } String everything=sb.tostring (); System.out.println (everything); }Catch(IOException e) {e.printstacktrace (); System.out.println ("IO Problem"); }finally{br.close (); } }}
exception handling in Java [Java]-cont.