如何在当前方法调用出现异常时停止执行后续的方法调用。

huangapple go评论80阅读模式
英文:

How to stop succeeding method calls executing if the current method call comes with an exception

问题

从我的主要JAVA类中,我在另一个类中调用了几个其他方法,其中任何一个方法调用都可能出现异常。我需要知道编程的最简单方法,以停止执行其余的方法调用,打印错误消息,并在当前方法调用出现异常时继续for循环的下一次迭代。

假设我的方法在类testConnection中进行了定义。

方法分别用A、B、C、D、E、F表示,在testConnection类中。

假设我的主类按照以下顺序调用这些方法。

for (int i = 0; i <= 5; i++) {
    try {
        testConnection.A();
        testConnection.B();
        testConnection.C();
        testConnection.D();
        testConnection.E();
        testConnection.F();
    } catch (Exception e) {
        System.err.println("出现异常:" + e.getMessage());
        continue;
    }
}

例如,如果B方法调用出现异常,我需要停止执行其余的方法(C、D、E和F),打印错误消息,并返回到for循环,继续下一次迭代。

英文:

From my main JAVA class I am calling several other method calls in an another class in which any of these method call can come up with an exception. I need to know the simplest way of programming to stop executing rest of the method calls, print an error message and continue the next iteration in the for loop if the current method call comes up with an exception.

Say my methods are defined in class - testConnection

Methods are denoted as A,B,C,D,E,F in class testConnection

Say my main class calling these methods as in below order.

for (int i =0; i&lt;=5; i++) {
    testConnection.A();
    testConnection.B();
    testConnection.C();
    testConnection.D();
    testConnection.E();
    testConnection.F();    
}

For an example, if B method call comes up with an exception, I need to stop rest of the methods executing (C,D,E and F), print an error message and return to the for loop and continue the next iteration

答案1

得分: 1

你应该将你的代码(方法调用)放在try块中,并在catch块中处理,就像这样。

for (int i = 0; i <= 5; i++) {
    try {
        testConnection.A();
        testConnection.B();
        testConnection.C();
        testConnection.D();
        testConnection.E();
        testConnection.F();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文:

you should keep your code(Method calling) into try block and handle it into a catch block Like this.

for (int i =0; i&lt;=5; i++) {
try{
testConnection.A();
            testConnection.B();
            testConnection.C();
            testConnection.D();
            testConnection.E();
            testConnection.F();    
        }catch(Exception e){
             System.out.println(e.printStackTrace());
        }

答案2

得分: 0

在循环内部添加 try/catch 块,这将会取消同一次迭代中的任何其他方法调用,但仍会继续执行循环。

for (int i = 0; i <= 5; i++) {
    try {
        testConnection.A();
        testConnection.B();
        testConnection.C();
        testConnection.D();
        testConnection.E();
        testConnection.F();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
英文:

Add a try/catch inside the loop, this will cancel any other method calls in the same iteration but still continue the loop

for (int i = 0; I &lt;= 5; i++) {
    try {
        testConnection.A();
        testConnection.B();
        testConnection.C();
        testConnection.D();
        testConnection.E();
        testConnection.F();    
    catch (Exception e) {
        System.out.println(e.getMessage());
    }
} 

答案3

得分: 0

如果你的任何方法抛出异常,除非你专门处理了异常,否则控制权将立即跳出。因此,如果方法 B 在方法中抛出异常,它将停止执行后续方法。你可以在父方法中捕获异常以进行优雅的处理。

在你的示例中,你可以进行以下操作:-

try {
    for (int i = 0; i <= 5; i++) {
        testConnection.A();
        testConnection.B();
        testConnection.C();
        testConnection.D();
        testConnection.E();
        testConnection.F();
    }
} catch (Exception e) {
    System.out.println("由于某些错误,执行在中途停止");
}

假设方法 B 抛出了某些异常。

英文:

If any of your methods throws an exception the control will immediately come out unless you have specifically handled the exception. So if method B has thrown the exception in the method and it will stop executing subsequent methods. You can catch the exception in your parent method to handle gracefully.

In your example you can do the following:-

try {
    for (int i =0; i&lt;=5; i++) {
     testConnection.A();
     testConnection.B();
     testConnection.C();
     testConnection.D();
     testConnection.E();
     testConnection.F();    
     }
}catch (Exception e){
System.out.println(&quot;Execution stopped mid way due to some error&quot;);
}

Assume method B has thrown some exception.

huangapple
  • 本文由 发表于 2020年5月2日 15:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61556148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定