Java 异常处理 try catch inside catch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3526167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:31:35  来源:igfitidea点击:

Exception handling try catch inside catch

javaexceptionexception-handlingtry-catch

提问by topgun_ivard

I recently came across code written by a fellow programmer in which he had a try-catch statement inside a catch!

我最近遇到了一位程序员同事编写的代码,其中他在 catch 中有一个 try-catch 语句!

Please forgive my inability to paste the actual code, but what he did was something similar to this:

请原谅我无法粘贴实际代码,但他所做的与此类似:

try
{
 //ABC Operation
}
catch (ArgumentException ae)
{
   try
   {
      //XYZ Operation
   }
   catch (IndexOutOfRangeException ioe)
   {
      //Something
   }
}

I personally feel that it is some of the poorest code I have ever seen! On a scale of 1 to 10, how soon do you think I should go and give him a piece of my mind, or am I over-reacting?

我个人觉得这是我见过的最糟糕的代码之一!在 1 到 10 的范围内,你认为我应该多久去给他一点意见,或者我反应过度?

EDIT: What he is actually doing in the catch, he is performing some operations which can/should be done when the initial try fails. My problem is with having a clean code and maintainability. Delegating the exception from the first catch to a different function or the calling function would be OK, but adding more code which may or may not throw an exception into the first catch, is what I felt was not good. I try to avoid multiple stacked "if-loop" statements, I found this equally bad.

编辑:他在捕获中实际在做什么,他正在执行一些可以/应该在初始尝试失败时完成的操作。我的问题是拥有干净的代码和可维护性。将第一个 catch 中的异常委托给不同的函数或调用函数是可以的,但是添加更多可能会或可能不会将异常抛出到第一个 catch 中的代码是我觉得不好的。我尽量避免多个堆叠的“if-loop”语句,我发现这同样糟糕。

采纳答案by x0n

Why is that bad? It's no different conceptually than:

为什么那么糟糕?它在概念上与以下没有什么不同:

void TrySomething() {
   try {


   } catch (ArgumentException) {
        HandleTrySomethingFailure();
   }
}

void HandleTrySomethingFailure() {
    try {

    } catch (IndexOutOfRangeException) {

    }
}

Before you go over there and give him a piece of your brain (try the parietal lobe, it's particularly offensive) , what exactly are you going to say to him? How will you answer the proverbial "why?"

在你去那里给他一块你的大脑之前(试试顶叶,它特别令人反感),你到底要对他说什么?您将如何回答谚语中的“为什么”?

What's even more ironic is that when the jitter inlines this code, it will look exactly like your example.

更具有讽刺意味的是,当抖动内联此代码时,它看起来与您的示例完全一样。

-Oisin

-Oisin

回答by Brian Agnew

Without knowing what the code does it's impossible to say. But it's not unusualto do this.

不知道代码是做什么的,就不可能说出来。但这样做并不罕见

e.g. if you have to clear up resources whilst handling exceptions, that clear-up code itself may have the capability to throw exceptions.

例如,如果您在处理异常时必须清除资源,则该清除代码本身可能具有抛出异常的能力。

回答by Colin Hebert

Here is a case :

这是一个案例:

try{
    //Dangerous Operation
} catch (AnyException ae) {
    try {
        //Do rollback which can fail
    } catch (RollbackFailedException rfe) {
        //Log that
    }
} finally {
    try {
        //close connection but it may fail too
    } catch (IOException ioe) {
        //Log that
    }
}

It's about the same thing as @x0n said. You might need to handle exception while try to close resources or while you're trying to resolve a situation brought by another exception.

这与@x0n 所说的差不多。在尝试关闭资源或尝试解决由另一个异常带来的情况时,您可能需要处理异常。