代码之家  ›  专栏  ›  技术社区  ›  Tilo Prütz

在finally块中访问objective-c异常

  •  3
  • Tilo Prütz  · 技术社区  · 17 年前

    鉴于以下情况:

    @try {
        @try {
            // raises an exception :)
            [receiver raisingFirstException];
        } @finally {
            // raises another exception :)
            [otherReceiver raisingFinalException];
        }
    } @catch (id e) {
        printf("exception: %s\n", [[e stringValue] cString]);
    }
    

    @finally 块或以获取 @catch

    我有密码 异常,但我不想失去原始异常(根本原因)。

    他们抛出的例外。

    1 回复  |  直到 17 年前
        1
  •  2
  •   Sebastian Celis    17 年前

    最好的方法是将异常分配给一个可以从块的其余部分访问的变量。

    NSException *ex;
    @try {
        @try {
            [someObject methodWhichCouldThrowException];
        } @catch (NSException *e) {
            ex = e;
        } @finally {
            [anotherObject methodWhichCouldThrowADifferentException];
        }
    } @catch (NSException *e) {
        // From here you can access both the exception thrown by 'someObject'
        // as well as the exception thrown by 'anotherObject'.
    }
    
    推荐文章