Java-exception handling

Check for exceptions: take the initiative to deal with

  1. Catch: use try catch block processing
  2. Throw: Declare possible exceptions on the method, and the caller will handle them.
    Common exceptions:
    Insert picture description here

try …catch…

public class Test {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            System.out.println(1 / 0);
        } catch (Exception e) {
    
    
            // 打印异常跟踪的信息
            e.printStackTrace();
            System.out.println("分母为零");
        }
        finally{
    
    
            System.out.println("end...");
        }
    }
}

result:

java.lang.ArithmeticException: / by zero
	at com.cao.test.lesson04.Test.main(Test.java:6)
分母为零
end...

You can also enter different catch blocks according to the type of exception

public class Test {
    
    
    public static void main(String[] args) {
    
    
        try {
    
    
            System.out.println(1 / 1);
            int[] arr=new int[2];
            System.out.println(arr[2]);
        } catch (ArithmeticException a) {
    
    
            // 打印异常跟踪的信息
            a.printStackTrace();
            System.out.println("分母为零");
        }catch (ArrayIndexOutOfBoundsException A){
    
    
            A.printStackTrace();
            System.out.println("数组越界...");
        }
        finally{
    
    
            System.out.println("end...");
        }

    }
}

Results of the:

1
数组越界...
end...
java.lang.ArrayIndexOutOfBoundsException: 2
	at com.cao.test.lesson04.Test.main(Test.java:8)

Sometimes exceptions are used to branch

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        try{
    
    
            System.out.println(1/1);
            System.out.println(args[0]);
        }catch (ArithmeticException A){
    
    
            System.out.println("-------");
        }catch (ArrayIndexOutOfBoundsException AA){
    
    
            System.out.println("+++++++");
        }
    }
}

Results of the:

1
+++++++

throws statement:

public class Test {
    
    
    public void loadClass() throws ArithmeticException {
    
    
        System.out.println(1/0);
    }
}
public class Start {
    
    
    public static void main(String[] args) {
    
    
        Test test = new Test();
        try {
    
    
            test.loadClass();
        } catch (ArithmeticException e) {
    
    
            System.out.println("error...");
            e.printStackTrace();
        }
    }
}
error...
java.lang.ArithmeticException: / by zero
	at com.cao.test.lesson04.Test.loadClass(Test.java:6)
	at com.cao.test.lesson04.Start.main(Start.java:9)

A place to pay attention

public class Test2 {
    
    
    int i=1;
    public int show(){
    
    
        try {
    
    
            System.out.println(1 / 0);
            System.out.println("......");
            return ++i;
        } catch (ArithmeticException A){
    
    
            System.out.println("捕获异常...");
            return ++i;
        }finally {
    
    
            System.out.println("end...");
            return ++i;
        }
    }

    public static void main(String[] args) {
    
    
        Test2 test2=new Test2();
        int res=test2.show();
        System.out.println(res);
        System.out.println(test2.i);
    }
}

result:

捕获异常...
end...
3
3

At catch (ArithmeticException A){ System.out.println("捕获异常..."); return ++i;}the time, it was executed ++ibut not return.


Custom exception:

1. Built-in exceptions cannot always be enough to catch all errors, so user-defined exception classes are required;
2. User-defined exception classes should be subclasses of Exception class (or a subclass of Exception class);
3. Anything created User-defined exception classes can obtain the methods defined by the Throwable class;

CustomException:

public class CustomException extends RuntimeException{
    
    
    public CustomException(){
    
    

    }
    public CustomException(String message){
    
    
        super(message);
    }
}

User:

public class User {
    
    
    public void login(String userName){
    
    
        if(userName=="admin"){
    
    
            throw new CustomException("用户名不能为admin...");
        }
        else{
    
    
            System.out.println("登陆成功...");
        }
    }

    public static void main(String[] args) {
    
    
        User user=new User();
        user.login("admin");
    }
}

result:

Exception in thread "main" com.cao.test.lesson04.demo.CustomException: 用户名不能为admin...
	at com.cao.test.lesson04.demo.User.login(User.java:6)
	at com.cao.test.lesson04.demo.User.main(User.java:16)

If User is:

public class User {
    
    
    public void login(String userName) {
    
    
        if (userName == "admin") {
    
    
            throw new CustomException("用户名不能为admin...");
        } else {
    
    
            System.out.println("登陆成功...");
        }
    }

    public static void main(String[] args) {
    
    
        User user = new User();
        try {
    
    
            user.login("admin");
        } catch (CustomException c) {
    
    
            System.out.println("用户名不能为admin...");
        }
    }
}

The result is:

用户名不能为admin...

The difference between throw and throws:

1) Throw is a statement that throws an exception; throws is a method that throws an exception; throws can be used alone, but throw cannot;
2) throws appear at the head of the method function; and throw appears at the function body;
3) throws indicate abnormalities There is a possibility that these exceptions may not necessarily occur; throw is an exception, and execution of throw must throw some kind of exception;
4) Both are passive ways of handling exceptions (negativity here does not mean This method is not good), it just throws or may throw an exception, but the function will not handle the exception. The actual handling of the exception is handled by the upper call of the function.

Guess you like

Origin blog.csdn.net/qq_44371305/article/details/113357772