【发布时间】:2011-01-24 14:02:30
【问题描述】:
是否可以在运行 try-catch 块的当前类中捕获方法?例如:
public static void arrayOutOfBoundsException(){
System.out.println("Array out of bounds");
}
.....
public static void doingSomething(){
try
{
if(something[i] >= something_else);
}
catch (arrayOutOfBoundsException e)
{
System.out.println("Method Halted!, continuing doing the next thing");
}
}
如果这是可能调用 catch 方法的正确方法是什么?
如果这是不可能,谁能指出我正确的方向,如何阻止异常停止我在 Java 中的程序执行而无需创建任何包中的新类,或修复产生 ArrayOutOfBoundsException 错误的代码。
提前致谢,
Java 新手
【问题讨论】:
-
catch子句查找异常 class 而不是 method。 因此参数中的e是一个Exception对象(或 类)
标签: java arrays exception-handling error-handling try-catch