Java's exception handling (throws)

com.atguigu.java1 Package; 

Import java.io.File;
Import a java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;

/ *
* exception handling two ways: throws + Exception Type
*
* 1. "throws + exception type" written method statement at. When indicating that this method is performed, the type of exception might be thrown.
* Once when performing the method body, abnormal, the object will generate an exception when the error code of the class, this object is satisfied after throws exception
* time type, it will be thrown. Exception codes follow the code will no longer perform!
*
* 2. Experience: try-catch-finally: The real exception to dispose of.
* Throws a way only exception thrown to the caller of the method. It does not really get rid of the abnormal.
*
* 3. How to develop, select Use try-catch-finally or the use of throws?
* 3.1 If the parent class is not being overwritten manner throws exception Subclasses override method can not be used throws, meaning that if
* subclass overrides the method there is an abnormality, must try-catch-finally approach.
* 3.2 performed in a method, has also called several other methods, these methods are progressive implementation of relations. We recommend the use of these methods throws
* way process. The method is performed using a conceivable embodiment try-catch-finally processed.
*
* /
Public class ExceptionTest2 {


public static void main (String [] args) {
the try {
method2 ();

} the catch (IOException E) {
e.printStackTrace ();
}

// the method3 ();

}


public static void the method3 ( ) {
the try {
method2 ();
} the catch (IOException E) {
e.printStackTrace ();
}
}


public static void method2 () throws IOException {
the method1 ();
}


public static void method1() throws FileNotFoundException,IOException{
File file = new File("hello1.txt");
FileInputStream fis = new FileInputStream(file);

int data = fis.read();
while(data != -1){
System.out.print((char)data);
data = fis.read();
}

fis.close();

System.out.println("hahaha!");
}


}

Guess you like

Origin www.cnblogs.com/wpy188/p/12088952.html