Java's exception handling (try-catch)

com.atguigu.java1 Package; 

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

Import org.junit.Test;

/ *
* a, exception handling : grasping parabolic model
*
* process a: "throw": during normal program execution, once an abnormality occurs, an error code will be generated at an object class corresponding to the exception.
* And throw this object.
* Once an object is thrown, followed by the code is no longer executed.
*
* Exception object generated on: ① the system automatically generates exception object
* ② manually generate an exception object and throw (the throw)
*
* Process II: "grasping": a process can be understood as an abnormal manner: ① try- ② the finally-throws the catch
*
*
* Second, the use of the try-catch-finally
*
* the try {
* // possible exception code
*
*} the catch (exception type variable name 1 1) {
* // handled exception. 1
*} the catch (Exception Type 2 2 Variable name) {
* // handled exception 2
*} the catch (Exception Type 3 Variable Name 3) {
* // to handle exceptions 3
*}
.... *
* the finally {
* // code that will perform
*}
*
* Description:
* 1. the finally is optional.
* 2. abnormal codes may try to wrap up, during execution, once the abnormal, generates an object corresponding to the exception class as this object
* type, to match the catch
* 3. Once the try the exception object when a match to a catch, the catch proceeds to abnormality processing. Once the process is complete, it jumps out of the current
* try-catch structure (in the absence of written finally). Then proceed code
* 4. catch the exception type if no parent child relationship, who declared on, it does not matter who the next statement.
* Catch the exception if the type of parent child relationship is satisfied, the above requirements subclass must declare in the parent class. Otherwise, an error
* The usual way of handling exception object: ① () ② printStackTrace () String getMessage
* 6. variables declared in the structure try, try again the structure of the future, no longer be invoked
* 7. try-catch-finally structure can be nested
*
* Experience 1: Use the try-catch-finally dealing with compiling an exception, the program will no longer have to be an error at compile time, but may still be an error running.
* We use the equivalent of an exception try-catch-finally will likely occur a compile-time, the delay appears to run.
*
* Experience 2: development, due to the more common runtime exceptions, so the exception write a try-catch-finally we usually do not run for time.
* For at compile-time exception, you're saying that we must consider exception handling.
* /
Public class ExceptionTest1 {


@Test
public void test2 () {
the try {
File = new new File ( "hello.txt") File;
the FileInputStream new new FIS = the FileInputStream (File);

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

fis.close ();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}

@Test
public void test1(){

String str = "123";
str = "abc";
int num = 0;
try{
num = Integer.parseInt(str);

System.out.println("hello-----1");
}catch(NumberFormatException e){
// System.out.println("出现数值转换异常了,不要着急....");
//String getMessage():
// System.out.println(e.getMessage());
//printStackTrace():
e.printStackTrace();
}catch(NullPointerException e){
System.out.println ( "null pointer exception, and do not worry ....");
} the catch (Exception E) {
System.out.println ( "abnormal, and do not worry ....");

}
System.out.println (NUM);

System.out.println ( "Hello ----- 2");
}

}

Guess you like

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