结论:
①throw+ an instance of Exception Class
②throws+ Exception Class
举例:
①throw+ an instance of Exception Class
throw new ArithmeticException("Arithmetic Exception");
-------------------------------------------------------------------------------------------------------------------------------------------
Example2020.java
public class Example2020 {
void checkAge(int age) {
if (age < 18)
throw new ArithmeticException("you are not an adult");
else
System.out.println("you are an adult");
}
public static void main(String[] args) {
Example2020 obj = new Example2020();
obj.checkAge(12);
System.out.println("End.");
}
}
②throws+ Exception Class
throws ArithmeticException;
public class Example2021 {
int division(int a, int b) throws ArithmeticException {
int t = a / b;
return t;
}
public static void main(String[] args) {
Example2021 obj = new Example2021();
try {
System.out.println(obj.division(12, 0));
} catch (ArithmeticException e) {
System.out.println("you should not divide number by zero");
}
}
}