Package Lianxi3;
Exception handling method Two: When this method has an exception, throws an object of the exception class, throwing the call to the method
//person. the object of the exception can be thrown up and down, until main, in the process of throwing, can also be used Try-catchImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classTestException { Public Static voidMain (string[] args) {Try{method2 (); }Catch(FileNotFoundException e) {System.out.println (E.getmessage ()); }Catch(IOException e) {e.printstacktrace (); }} Public Static voidMETHOD2 ()throwsfilenotfoundexception,ioexception{method1 ();} Public Static voidMethod1 ()throwsfilenotfoundexception,ioexception{FileInputStream fis=NewFileInputStream (NewFile ("Hello.txt")); intstr; while((Str=fis.read ())!=-1) {System.out.print (Char) str); } fis.close ();} }
Exception handling three: manually throwing exceptions
The type of exception thrown, if RuntimeException can not be displayed for processing.
If a exception, it must be handled explicitly. such as throw new RuntimeException ("The incoming type is wrong!") ”);
Java Exception handling 2