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

Java是否支持在异常发生后恢复程序执行?,java,exception-handling,Java,Exception Handling,我想知道如何在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.charA

我想知道如何在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");
        }
    }
}
印行如下:

a
j
the index is out of bounds
a
j
the index is out of bounds
is it still running
但抛出错误后,我希望代码继续运行,以便打印:

a
j
the index is out of bounds
a
j
the index is out of bounds
is it still running

提前感谢

Java不支持异常后的“恢复”或“重新启动”

您可以在
try/catch
中包装特定的行“toskip”(在上面的示例中,总共是3行,每个访问一行),或者更好的做法是编写不会引发异常的代码——异常应该是“异常”IMHO。您也可以将
try/catch
代码移动到方法中以“包装”访问(例如调用方法3x),但操作相同。

for(int i=0;ifor(int i =0; i < Short.MAX_VALUE; i++){
 try{
    System.out.println(str.charAt(i));
 }catch(Exception ex){}
}
试一试{ 系统输出println(str.charAt(i)); }捕获(例外情况除外){} }
如果希望始终执行,也可以使用finally bolck。

哈哈+但是我不想打印任何值,而且最好是使i