throw new RuntimeException(“…..”);
抛出运行时异常,方法上就不需要加throw Exception。而且直接终止程序。
public static void main(String[] args) {
int i=3,j=0;
try {
int s = i/j;
System.out.println(s);
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException("stop");
}
System.out.println(11111);
}
这段代码显然会抛出异常,如果不加 throw new RuntimeException(“stop”); 程序的结果还是会打印 “11111”;反之如果加上,则程序终止。