public class Test_Exception {
public static void main(String[] args){
int a = 1;
int b = 1;
add(a,b);
}
public static void add(int a,int b) throws RuntimeException{
if (a == b)
throw new RuntimeException("Error!!");
}
}
public class Test_Exception {
public static void main(String[] args){
int a = 1;
int b = 1;
add(a,b);
}
public static void add(int a,int b) throws RuntimeException{
if (a == b)
throw new RuntimeException("Error!!");
}
}
throws关键字写在函数名和形参列表后面以及函数体的前面,用于声明该函数将要抛出指定类型的异常,比如上文实例代码中,add函数将会抛出一个RuntimeException异常。
throw关键字用于抛出一个特定的异常,如在实例代码的add函数中,如果a等于b,就会抛出一个带有"Error"提示信息的RuntimeException异常。