[Java] Exception handling

In a Java program, an abnormal situation that occurs is called an exception

-[Logical error and grammatical error] is not abnormal!

-Divided into Error and Exception

-Error is an error problem that the JVM cannot solve, such as memory overflow StackOverFlow and OOM

-Exception generally handles unexpected problems

For example, null pointer NullPointerException, array out of bounds OutofIndexException, connection timeout exception, no exception can be read

 

For exception handling, 1 terminates the program and 2 sets the possibility of exceptions when writing

 

Compile-time exceptions and runtime exceptions

Exception checked at compile time

 

Runtime exception UnChecked

 

 

Two processing mechanisms Try-Catch-Finally Throws-Throw

 

 -Handle exceptions that may exist at compile time to the runtime

// abnormal program execution species occurs, an error code corresponding to the generated exception object and throws an exception class
     // once the exception object is thrown, runs terminate 
    @Test
     public  void exceptionTest1 () {
         the try {
             // possible exception Code 
        } catch (Exception exception) { // Exceptions that occur when matching parameters, enter the catch code block to handle exceptions
             // The way to handle exceptions 
            exception.printStackTrace (); 
        } finally {
             // No matter whether an exception occurs, it must eventually The executed code block
             // applies to the code that must be executed at the end, such as releasing resources 
            System.out.println ("Must be executed code!" ); 
        } 
        //In addition to the direct system finally exits the try, will be executed
         // if the try in the end emerged return method, have to wait finally finished, go back to try to perform return 
    } 

    @Test 
    public  void exceptionTest2 () {
         try { 
        } finally { // You can execute finally directly without handling exceptions, but do n’t use it 
        } 
    } 

    @Test 
    public  void exceptionTest3 () {
         try {
             // Can catch multiple exceptions one by one 
        } catch (NullPointerException nullPointerException) { 
            nullPointerException.printStackTrace () ; 
        } catch(ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { 
            arrayIndexOutOfBoundsException.printStackTrace (); 
        } catch (ArithmeticException arithmeticException) { 
            arithmeticException.printStackTrace (); 
        } catch (Exception exception) {
             // When all exceptions are not caught, finally hand over to the total exception handling 
            exception. printStackTrace (); 
        } 
        // Do not write the total exception catch at the beginning, after the total exception is directly caught and processed, the following detailed exceptions cannot be caught, written in white 
    }
TRY - CATCH - FINALLY

 

-Continue to throw upward

    // If we don't use try-catch-finally for processing, we can directly throw an exception 
    
    // throws + the corresponding exception type, declare the exception that may occur in this method, if an exception occurs, throw the exception up
     // essentially, throws does not solve the exception, but throws the exception to the caller of the release method. The caller can try-catch or continue to throw up.
     // When an exception occurs, an exception object is generated at the exception code, if this exception meets When throws contains an exception type, the exception object throws 
    public  void throwsMethod () throws Exception { 
       
    }
THROWS

 

scenes to be used:

-The overridden method in the parent class does not throw an exception, and the subclass cannot use throws. If the overridden method of the subclass fails, it must be handled by try-catch

-Multiple other methods are called in method a, which is a progressive relationship. The called method should declare throws and throw them together in the try-catch method of method a.

 

-THROW manually creates exceptions and throws exceptions

    void throwException(int age) throws Exception {
        if (0 < age || age > 100) throw new Exception("不合理的年龄");
    }
THROW

 

 

Custom exception class

/ * 
Compile-time exception Exception 
RuntimeException 
 * / 

// Custom exception needs to inherit the exception class 
public  class WTF_Exception extends Exception { 

    // Unknown constant ID 
    static  final  long serialVersionUID = -3387516993124229948L ; 
    
    public WTF_Exception () { 
        
    } 
    
    public WTF_Exception ( String message) {
         super (message); 
    } 
}
View Code

Serialize UID to save record identification value?

https://blog.csdn.net/seabreezesuper/article/details/70141844

 

Guess you like

Origin www.cnblogs.com/mindzone/p/12725913.html