Throw and throws

1. Throw (the statement throws an exception)

throw <exception object>

throw new Exception();

This statement will throw an exception 

2. Throws (the method throws an exception)

<modifier><return type><method name><parameter list>throws exception class {}

public static void div(int b)throws Exception{}

 This method will throw an exception 

three.

throws can be used alone

throw cannot be used alone

1. If the method uses throws, then throw can be used directly in the method

2. If the method does not use throws, then throw should use try{}catch{} statement

public static void div(int b){
        if(b==0) {
            try {
                throw new Exception("分母不可为0");
            } catch (Exception e) {
                e.printStackTrace();
            }        
        }
    }

Guess you like

Origin blog.csdn.net/SignalFire/article/details/105442616