Java study notes of exception handling

Abnormal: Error Exception

Overview:
the try block {1}: code for monitoring a code block
catch () {} block 2: 1 occurs when the code corresponding to the captured content exception block corresponding thereto is performed.
} finally {3 block: represents any case, whether there is abnormality, the contents of the code are executed inside.

Abnormal: block 1 ---> Code Block 2 ----> 3 code block (. After abnormal, behind the exception code is not executed, the process changes the code)
did exception: block 1-- -> 3 code blocks

, we can capture a variety of anomalies, respectively, once an exception occurs that enters inside the corresponding catch block.
Abnormal captured sequence: first subclass to superclass crude after the first fine

Can not finally block? You can
can not do without a catch block? Finally it must not have a catch. If there can be no grammatical finally catch on, we still have to add CAT CH

Abnormal compile time: you need to catch an exception during compilation. That must try catch capture
run-time exception: NullPointerException. As it needed with or without.

1          the try {
 2              
. 3              // we want to monitor the following code
 4              // Code Block 1;
 5              // reference data type null, you can not call any of the above methods or properties null. Otherwise, given a null pointer exception. A NullPointerException
 . 6  //             String S = null;
 . 7  //             s.equals ( "ABC");
 . 8              
. 9  //             System.out.println (5/0); 
10              
. 11              
12 is              System.out.println ( "Hello" );
 13 is          } the catch (NullPointerException E) {
 14              // block 2; // only occurred when it performs NullPointerException 
15             System.out.println ( "your code appears ..... a NullPointerException" );
 16  //             e.printStackTrace (); 
. 17          } the catch (an ArithmeticException E) {
 18 is              // code block 2; // only appeared ArithmeticException when it is executed 
. 19              System.out.println ( "your code appears ..... ArithmeticException" );
 20 is  //             e.printStackTrace (); 
21 is          } the finally {
 22 is              // code block. 3; 
23 is              the System. out.println ( "This code is always executed it really?" );
 24          }

Custom exception:

A custom exception class inherits Exception Interface

 

public class OutOfAgeException extends Exception{
    
    /**
     * Custom exception handling
     */
    private static final long serialVersionUID = 1L;
    
    public OutOfAgeException(String msg){
        super(msg);
        
    }
    

}

 

Write a method throws an exception

 

public static void age(int age) throws OutOfAgeException {
        if(age<0){
            throw new OutOfAgeException("年龄不合法...");
        }else{
            System.out.println ( "legal age" );
        }
    }

 

In one method call on the line block appeal

 

java exception throw and throws difference:

throws: used to declare all exceptions, no treatment but rather an exception to upload a method that may arise, who calls me I'll throw anyone.

throw: it is used to a particular type of exception thrown.

 

Guess you like

Origin www.cnblogs.com/sunzhiqiang/p/11742884.html