2

Java 异常处理流程

 2 years ago
source link: https://jack-zheng.github.io/hexo/2022/02/21/Java-exception/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

今天在写一段补偿代码的时候突然想到一个问题,当异常发生时,后续代码是否还会被执行的问题。测试前根据主管猜测,感觉如果有 try-catch, 那么会继续执行;没有则直接跳出了,相当于 return.

Scenario 1

public class Test1 {
public static void main(String[] args) {
try {
int a = 1/0;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Log after exception");
}
}

// > Task :idl-sfutil:idl-sfutil-service:Test1.main()
// Log after exception

抛错后继续执行

Scenario 2

public class Test1 {
public static void main(String[] args) {
int a = 1/0;
System.out.println("Log after exception");
}
}

// Exception in thread "main" java.lang.ArithmeticException: / by zero
// at com.sf.sfv4.util.Test1.main(Test1.java:5)

抛错后没有执行


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK