简体   繁体   English

异常处理执行流程

[英]Exception handling flow of execution

I should know this, but for some reason I can't figure it out right now.我应该知道这一点,但由于某种原因,我现在无法弄清楚。

Is there a way to rewrite this code to avoid instanceof ?有没有办法重写这段代码以避免instanceof

try {
    //Exceptions may happen
} catch (Exception1 exception1) {
    //do stuff only in case of Exception1
} catch(Exception e) {
    // do stuff in all exception cases except Exception1
    if (e instanceof Exception2) {
        // do stuff only in case of Exception2
    }
}

If you're going to have your exception handling for Exception inline, and don't want to repeat it, then the only solution is the OP solution.如果您打算对Exception inline 进行异常处理,并且不想重复它,那么唯一的解决方案就是 OP 解决方案。

However, if you can put the common exception handling into a function, then this would be neat:但是,如果您可以将常见的异常处理放入 function 中,那么这将是整洁的:

try {
    //Exceptions may happen
} catch (Exception1 exception1) {
    //do stuff only in case of Exception1
} catch(Exception2 e) {
    doCommonStuff();
    doException2Stuff();
} catch(Exception e) {
    doCommonStuff();
}

However, the above may lead to a small problem in terms of whether you want to pass control to outside of the try/catch block, or whether you're hoping to return a value, or even throw another exception.但是,上述情况可能会导致一个小问题,即您是否希望将控制权传递到 try/catch 块之外,或者您是否希望返回一个值,甚至抛出另一个异常。

You may find that although all paths of your code DO throw an exception, that the above construct, of using common exception handler methods, may look to the compiler like some control paths don't.您可能会发现,尽管您的代码的所有路径都会引发异常,但上述使用常见异常处理程序方法的构造可能会像某些控制路径一样看起来像某些控制路径一样。

You can, therefore, express a common exception handler, that always throws an exception like this:因此,您可以表达一个通用的异常处理程序,它总是会抛出这样的异常:

private <T> T throwLastException(Exception2 input) {
   // blah blah blah
   throw new SomeException();
}

and this can be called as part of a return statement that appears to guarantee the calling function returns a value, when in fact it's just telling the compiler not to worry:)这可以作为 return 语句的一部分调用,该语句似乎保证调用 function 返回一个值,而实际上它只是告诉编译器不要担心:)

Without any further context IMO the following is good enough:如果没有任何进一步的上下文 IMO,以下内容就足够了:

try {
    //Exceptions may happen
} catch (Exception1 exception1) {
    //do stuff only in case of Exception1
} catch (Exception2 exception2) {
    some_method();
}
catch(Exception e) {
    some_method();
}

Where some_method would encapsulate the code related with "// do stuff in all exception cases except Exception1"其中 some_method 将封装与“// do stuff in all exception cases except Exception1”相关的代码

Consider encapsulating common code between catch clauses 2 & 3 on some method, for instance:考虑在某些方法的 catch 子句 2 和 3 之间封装公共代码,例如:

try {
   //code
} catch (Exception1 e1) {
   // specific code for exception 1
} catch (Exception2 e2) {
   // specific code for exception 2
   method();
} catch (Exception e) {
   method();
}

Catch two types of exception in one block with the "|"用“|”在一个块中捕获两种类型的异常operator.. And maybe you can execute code that you have to execute in any case in the finally statement.运算符..也许您可以在 finally 语句中执行无论如何都必须执行的代码。 That are all options you have, and i think creating a method and calling it isn't a option, when you're trying to make your code more readable, what I think you're trying to do这就是你拥有的所有选项,我认为创建一个方法并调用它不是一个选项,当你试图让你的代码更具可读性时,我认为你正在尝试做的事情

try {
     //code
} catch (Exception1 | Exception2 e2) {
    // specific code for exception 1 & 2
} catch (Exception e) {
    // handle other exceptions
} finally {
    // execute in any case 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM