如果finally块返回,Xcode调试器无法捕获异常

时间:2022-05-27 23:09:02

I discovered weird behavior of Xcode. Xcode debugger doesn't break for uncaught exception in this code.

我发现Xcode的奇怪行为。 Xcode调试器不会因此代码中的未捕获异常而中断。

@try            { @throw @"AA"; }
@catch (...)    { @throw;       }
@finally        { return;       }

But exception in this code caught and trigger Xcode break execution for debugging.

但此代码中的异常捕获并触发Xcode中断执行以进行调试。

@try            { @throw @"AA"; }
@catch (...)    { @throw;       }
@finally        {               }

If @finally block returns debugger can't catch the exception. Have you ever seen this problem? I'm not sure this is really an issue. By the perspective it looks like designed behavior. I don't know why. Shouldn't I return in @finally block? My problem is it swallows exception silently, so I can't detect it.

如果@finally阻止返回调试器无法捕获异常。你见过这个问题吗?我不确定这是一个真正的问题。从视角看,它看起来像设计的行为。我不知道为什么。我不应该在@finally块中返回吗?我的问题是它无声地吞下异常,所以我无法检测到它。

Shame on me, I don't know well try...catch...finally behaviors. I almost haven't used exception catching code. Is this designed behavior or buggy behavior? Is this any known issue?

对我感到羞耻,我不知道好好尝试......抓住......最后的行为。我几乎没有使用异常捕获代码。这是设计行为还是错误行为?这有什么问题吗?

Here's my environment.

这是我的环境。

  • Xcode Version 4.4 (4F250)
  • Xcode版本4.4(4F250)

  • OS X 10.7.4
  • OS X 10.7.4


Edit

I attach full test source code.

我附上完整的测试源代码。

#import <Foundation/Foundation.h>

int main (int a, char** b)
{
    @try
    {
        NSLog(@"trying something...");

        @try            { @throw @"AA"; }
        @catch (...)    { @throw;       }
        @finally        { return 0;     }
    }
    @catch (...)
    {
        NSLog(@"something catched.");
    }
    @finally
    {
        NSLog(@"finally...");
    }
}

1 个解决方案

#1


1  

Putting a return in a @finally block seems like a bad idea. The exception handling mechanism is going to try to unwind the call stack as it deals with the exception you're throwing. If a @finally block changes what's on the stack, you undermine the exception handler. It doesn't seem at all surprising that this crashes.

将回报放在@finally块中似乎是一个坏主意。异常处理机制将尝试解除调用堆栈,因为它处理您正在抛出的异常。如果@finally块改变了堆栈上的内容,则会破坏异常处理程序。这种崩溃似乎并不令人惊讶。

Also, as bbum pointed out, exceptions aren't used for flow control in Cocoa and Cocoa Touch. Throwing an exception through a Cocoa method usually fails. Even if what you're doing is supposed to work in generic Objective-C, it would probably still cause problems in real code.

另外,正如bbum所指出的,Cocoa和Cocoa Touch中的流量控制不使用异常。通过Cocoa方法抛出异常通常会失败。即使您正在做的事情应该在通用Objective-C中工作,它仍可能在实际代码中引起问题。

Short answer: Don't do that.

简短的回答:不要这样做。

#1


1  

Putting a return in a @finally block seems like a bad idea. The exception handling mechanism is going to try to unwind the call stack as it deals with the exception you're throwing. If a @finally block changes what's on the stack, you undermine the exception handler. It doesn't seem at all surprising that this crashes.

将回报放在@finally块中似乎是一个坏主意。异常处理机制将尝试解除调用堆栈,因为它处理您正在抛出的异常。如果@finally块改变了堆栈上的内容,则会破坏异常处理程序。这种崩溃似乎并不令人惊讶。

Also, as bbum pointed out, exceptions aren't used for flow control in Cocoa and Cocoa Touch. Throwing an exception through a Cocoa method usually fails. Even if what you're doing is supposed to work in generic Objective-C, it would probably still cause problems in real code.

另外,正如bbum所指出的,Cocoa和Cocoa Touch中的流量控制不使用异常。通过Cocoa方法抛出异常通常会失败。即使您正在做的事情应该在通用Objective-C中工作,它仍可能在实际代码中引起问题。

Short answer: Don't do that.

简短的回答:不要这样做。