简体   繁体   English

异常处理尝试捕获内部catch

[英]Exception handling try catch inside catch

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. 将第一个catch中的异常委托给另一个函数或调用函数就可以,但是添加更多代码可能会或者可能不会将异常抛入第一个catch中,这是我觉得不好的。 I try to avoid multiple stacked "if-loop" statements, I found this equally bad. 我试图避免多个堆叠的“if-loop”语句,我发现这同样糟糕。

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

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. 这和@ x0n说的差不多。 You might need to handle exception while try to close resources or while you're trying to resolve a situation brought by another exception. 在尝试关闭资源或尝试解决另一个异常带来的情况时,您可能需要处理异常。

Without knowing what the code does it's impossible to say. 不知道代码是什么,这是不可能的。 But it's not unusual to do this. 但这样做并不罕见

eg if you have to clear up resources whilst handling exceptions, that clear-up code itself may have the capability to throw exceptions. 例如,如果您在处理异常时必须清理资源,那么清理代码本身可能会抛出异常。

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

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