小编jav*_*ava的帖子

Java是否支持在异常后恢复程序执行?

我想知道如何在java中捕获错误但允许程序继续运行.

这是我的例子:

public class test1 {
    public static void main(String[] args) {
        String str = new String("abcdefghij");
        try {
            System.out.println(str.charAt(0));
            System.out.println(str.charAt(9));
            System.out.println(str.charAt(10));
            System.out.println("is it still running");
        } catch (Exception e) {
            System.out.println("the index is out of bounds");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

打印如下:

a
j
the index is out of bounds
Run Code Online (Sandbox Code Playgroud)

但是在抛出错误之后我希望代码继续运行以便打印出来:

a
j
the index is out of bounds
is it still running
Run Code Online (Sandbox Code Playgroud)

提前致谢

java exception-handling

2
推荐指数
1
解决办法
674
查看次数

标签 统计

exception-handling ×1

java ×1