PlayJava Day011

Today Plant Science:

/ * 2019.08.19 started to learn, to make this shift. * /

Java exception handling

1. unusual concept: the problem of the program is running, causing the interruption.

2. capture and handle exceptions: Java in, to catch the exception with a try ... catch ...; try ... catch ... finally

①try there may be abnormal write code statements, catch inside the capture and processing. To facilitate debugging, print out the stack information for tracking purposes.

②finally: even if there is return, we will certainly perform the back.

3.①throws represents the current method does not handle the exception, but to the call of the method to deal with. // throw up

Equivalent statement throws an exception. // throw an exception to the outside, do not want to deal with, throw the others.

例如:throws NumberFormatException

②throw represents a direct throw an exception.

Example:

public static void testThrow(int a) throws Exception {
    if(a == 1) {
        throw new Exception("有异常") ;
    }
    System.out.println(a) ;
    public static void main(String[] args) {
        try {
            testThrow(1) ;
        } catch(Exception e) {
             e.printStackTrace() ;
        }
         try {
            testThrow(0) ;
        } catch(Exception e) { 
             e.printStackTrace (); 
        }

4.①Exception are checked exceptions, compile-time checks. You must be used in the program try ... catch ... processing.

②RuntimeException non checked exceptions, no compile-time checking.

For example NumberFormatException, you can not use the try ... catch ... processing. However, if an exception, the exception is processed by the JVM.

It is best to use try ... catch ... also captured.

The custom exception class: define a subclass inherits from Exception.

E.g:

public class CustomException extends Exception {
    public CustomException(String message) {
        super(message) ;
    }  //构造方法
}

 

Guess you like

Origin www.cnblogs.com/JavaDemo01/p/11518364.html