我想知道如何在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)
提前致谢