Java serialization 62- Use keywords throws exception handling

One, two ways to handle the exception

Exceptions, requires 1. All the compile-time programmers programming stage, it must be treated, if not addressed, the compiler will not pass, there are two ways to handle exceptions: catch and throw statements.

2. Capture: try ..... catch .......

3. The position statement is to use the method declaration throws the keyword throws an exception is thrown.

Abnormality, the abnormality does not require programmers to write stage process it runs 4. RuntimeException subclasses are all running.

5. The abnormality probability of compile-time high; the probability of occurrence of abnormal running low.

6. All of a direct subclass of Exception is "compile-time anomaly."

Second, there are two ways to handle exceptions

1. Declare throw throws

2. Capture try ..... catch .......

The following program demonstrates the first approach: throw statement, using the throws keyword position on the method declaration thrown up.

 

Package com.bjpowernode.java_learning; 

Import the java.io. * ; 


public  class D62_1_ { 

  public  static  void main (String [] args) throws a FileNotFoundException { 

    // create a file input stream, read file 

    // Java compiler how the following code may know that there is an exception during the execution of 

    // the Java compiler is how to know the probability of occurrence of this anomaly is relatively high? 

    // the Java compiler is not so smart, because this FileInputStream constructor uses a position statement on the 

    // throws FileNotFoundException; 

    // FileInputStream FLS = new new FileInputStream ( "C: \\ \\ lenovo1 the Users \\ Desktop \\ micro-channel public operations -Java \\ Java serial number 51-super keyword the .docx "); 

//     the try { 

    M1 (); 

      //Use throws exception handling is not really handle the exception rather pass the buck 

      // Who will run to whom calls 

      // The above method m1 if abnormal, because the use is to throw up, to the JVM, JVM encountered this exception 

      // will exit JVM, the following code will not execute 

//     } the catch (a FileNotFoundException E) { 

//       System.out.println ( "abnormality has occurred"); 

//     } 

  } 

  public  static  void M1 () throws a FileNotFoundException { 

    M2 (); 

  } 

  public  static  void M2 () throws a FileNotFoundException { 

    M3 (); 

  } 

  public  static  void M3 () throws a FileNotFoundException { 

    //FileNotFound using the constructor declared position throws (throws up) 

    new new the FileInputStream ( "E: \\ \\ timg.jpgu 04.image" ); 

  } 

}

Abnormalities appeared to throw up layer by layer.

Look again using a try .... catch ...... treat exceptions

 

  public  static  void main (String [] args) { 

    // create a file input stream to read the file 

    // how java compiler is aware of the following codes there may be an exception during execution 

    // java compiler How do I know this is an exception occurs the probability is relatively high? 

    // the Java compiler is not so smart, because this FileInputStream constructor uses a position statement on the 

    // throws FileNotFoundException; 

    // FileInputStream FLS = new new FileInputStream ( "C: \\ \\ lenovo1 the Users \\ Desktop \\ micro-channel public No. operations -Java \\ Java serialized 51-super keyword .docx "); 

    the try { 

      M1 (); 

      // use throws exception handling is not really handle the exception rather pass the buck 

      // who will run to whom to call 

      // If the above method m1 abnormal, because the use of thrown up to the JVM, JVM encountered the exception 

      // will exit the JVM, the following code will not execute 

    } the catch (a FileNotFoundException E) {

      System.out.println ( "abnormality has occurred" ); 

    } 

  }

Third, the source code:

D62_1_ThrowsOfException.java

https://github.com/ruigege66/Java/blob/master/D62_1_ThrowsOfException.java

2.CSDN:https://blog.csdn.net/weixin_44630050

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12071024.html
Recommended