关于程序流程,这个问题已经困扰我一段时间了。
我想知道是否可以从方法中捕获错误,以阻止它执行通常遵循它的方法,如下例所示,我无法开始工作。
public class MyClass {
public static void main(String[] args) {
// this method catches an exception and stops running
method01();
// this method will continue anyway which I don't want
method02();
};
};
我通常会有一个静态 int 变量,当程序运行时它会初始化为 0,然后如果一个方法捕获到异常,它将增加该 int,并且每个方法仅在 int 为 0 时才运行。
这可行,但我只是想知道是否可以用异常处理替换 int shindig 。
你能试一下吗:
try {
method01()
} catch (final Exception e) {
// do something
return; ///stop processing exit
}
method01 将抛出异常:
private void method01() throws Exception {
// something
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)