问题
异常之后程序的运行情况,如果在循环中,不同的处理异常方式,程序是结束?继续?循环是继续?还是跳过?
结论
- 有捕获异常的程序可以继续执行,但是从异常代码这行开始直到try结束都不执行,出了try再继续执行
- 没捕获异常,就直接报错,不论是否抛出(不考虑抛出后在其他方法中进行捕获)
- 根据以上结论得出:如果是for-try,那么for可以完成全部循环,但是出异常的那次循环从异常代码那一行开始后面的就不执行了,直接进行下一次循环。如果是try-for,那么一个单独的for相当于没有捕获异常,for运行到异常后会直接中断,程序跳到try外面继续执行。而throws相当于什么都没干,继续传递异常等待调用它的方法去处理。
测试
不处理,循环内/外捕获,仅抛出,循环内捕获中抛出,循环外捕获中抛出…
不处理
异常结束,异常后的代码不执行
/**
* 出现异常什么都不做
*/
public void nothing(){
System.out.println("异常开始前");
for (int i = 0; i < 5; i++){
System.out.println("i = " + i);
if(i == 2){
System.out.println("出现异常...");
int j = 10 / 0;
System.out.println("异常的下一行...");
}
}
System.out.println("异常结束后");
}
------------什么都不做----------
异常开始前
i = 0
i = 1
i = 2
出现异常...
Exception in thread "main" java.lang.ArithmeticException: / by zero
at org.example.other.TryCatchAfterSomethings.nothing(TryCatchAfterSomethings.java:19)
at org.example.App.main(App.java:45)
Process finished with exit code 1
捕获-循环内
正常结束,for全部执行。但是for-try中异常后面的代码不会执行,抛出异常后,下一个循环正常运行
/**
* 捕获异常,try放在循环内
*/
public void catchExceptionInFor(){
System.out.println("异常开始前");
for (int i = 0; i < 5; i++){
System.out.println("i = " + i);
if(i == 2){
try {
System.out.println("出现异常...");
int j = 10 / 0;
System.out.println("异常的下一行...");
}catch (Exception e){
e.printStackTrace();
}
}
}
System.out.println("异常结束后");
}
------------捕获异常,try放在循环内----------
异常开始前
i = 0
i = 1
i = 2
出现异常...
i = 3
i = 4
异常结束后
java.lang.ArithmeticException: / by zero
at org.example.other.TryCatchAfterSomethings.catchExceptionInFor(TryCatchAfterSomethings.java:36)
at org.example.App.main(App.java:47)
Process finished with exit code 0
捕获-循环外
正常结束,for中断执行,但程序全部执行。try-for中异常触发,不会被处理,在try中异常后剩余代码不会执行,但出了try后继续执行
/**
* 捕获异常,异常放在循环外
*/
public void catchExceptionOutFor(){
System.out.println("异常开始前");
try {
for (int i = 0; i < 5; i++){
System.out.println("i = " + i);
if(i == 2){
System.out.println("出现异常...");
int j = 10 / 0;
System.out.println("异常的下一行...");
}
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("异常结束后");
}
------------捕获异常,try放在循环外----------
异常开始前
i = 0
i = 1
i = 2
出现异常...
异常结束后
java.lang.ArithmeticException: / by zero
at org.example.other.TryCatchAfterSomethings.catchExceptionOutFor(TryCatchAfterSomethings.java:58)
at org.example.App.main(App.java:51)
Process finished with exit code 0
仅抛出
异常结束,for中断执行。异常后的代码不执行,不考虑在调用时捕获的问题
/**
* 只抛出异常,不捕获,循环内
*/
public void throwException() throws Exception {
System.out.println("异常开始前");
for (int i = 0; i < 5; i++){
System.out.println("i = " + i);
if(i == 2){
System.out.println("出现异常...");
throw new Exception();
// throw后的代码直接提示报错
// System.out.println("异常的下一行...");
}
}
System.out.println("异常结束后");
}
------------只抛出异常,不捕获,循环内----------
异常开始前
i = 0
i = 1
i = 2
出现异常...
Exception in thread "main" java.lang.Exception
at org.example.other.TryCatchAfterSomethings.throwException(TryCatchAfterSomethings.java:79)
at org.example.App.main(App.java:54)
Process finished with exit code 1
抛出-循环内-捕获
正常结束,for全部执行。for-try-throw后不能写代码,所以抛出throw后的代码不执行,下一个循环正常运行,捕获之后方法上的throws Exception失效
/**
* 抛出异常,并捕获,在循环内
*/
public void throwAndCatchExceptionInFor() throws Exception {
System.out.println("异常开始前");
for (int i = 0; i < 5; i++){
System.out.println("i = " + i);
if(i == 2){
try {
System.out.println("出现异常...");
throw new Exception();
// throw后的代码直接提示报错
// System.out.println("异常的下一行...");
}catch (Exception e){
e.printStackTrace();
}
}
}
System.out.println("异常结束后");
}
------------抛出异常,并捕获,在循环内----------
异常开始前
i = 0
i = 1
i = 2
出现异常...
i = 3
i = 4
异常结束后
java.lang.Exception
at org.example.other.TryCatchAfterSomethings.throwAndCatchExceptionInFor(TryCatchAfterSomethings.java:97)
at org.example.App.main(App.java:57)
Process finished with exit code 0
抛出-循环外-捕获
正常结束,for中断执行。try在for外面,所以for中异常一次就直接失败,但是try后面方法继续执行
/**
* 抛出异常,并捕获,在循环外
*/
public void throwAndCatchExceptionOutFor() throws Exception {
System.out.println("异常开始前");
try {
for (int i = 0; i < 5; i++){
System.out.println("i = " + i);
if(i == 2){
System.out.println("出现异常...");
throw new Exception();
// throw后的代码直接提示报错
// System.out.println("异常的下一行...");
}
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("异常结束后");
}
------------抛出异常,并捕获,在循环外----------
异常开始前
i = 0
i = 1
i = 2
出现异常...
异常结束后
java.lang.Exception
at org.example.other.TryCatchAfterSomethings.throwAndCatchExceptionOutFor(TryCatchAfterSomethings.java:119)
at org.example.App.main(App.java:60)
Process finished with exit code 0
如果内容有错误,还请指出,谢谢!