objective-c - cocoa 中 try/catch 内部的错误传播

标签 objective-c cocoa exception try-catch

我想在方法的 catch block 内引发异常,并在调用较早方法的另一个方法的 catch 中处理该异常。 我尝试过这个逻辑-

method B()
{
    @try{
        .......
        // calling method where is probable chance of exception


    method A();
    }
    catch(NSException e)
    {
        //catching the exception thrown in the method B()
        NSString* theError=[e reason];
        NSLog(@"the error is == %@",theError);
    }
}

method A()
{
    @try{
        .............
        //throw an exception incase of some condition
        throw e;
    }
    catch(NSException e)
    {
        //rethrowing the exception, want to catch in the method from where this method is called.  
        throw e;
    }
}

但是方法 B() 的 catch block 永远无法访问。 控制永远不会返回到方法 B() 的 catch block 。 请提出建议。

谢谢, 苏丹苏

<小时/>

这里有一些代码。我正在从 MyController 调用 TableController 的方法(populateData)。 异常应该发生在 TableController(initializeTest) 的另一个方法中,我抛出它 在 FinderCompleted 方法的 try block 内。在同一方法的 catch block 中,重新抛出异常,如下 我不想在这里处理它。 控制仅限于方法的最里面的 catch block - (void)FinderCompleted:(id)args NSLog 打印如下 - 我在这里 1 错误里面 你的桶里什么也没有:D 第一次抛出异常 陷阱--第一次异常,第二次抛出它 错误是==发生了意外的事情--EXCEPTION

之后我不知道控件去了哪里。我希望控件去捕获调用 FinderCompleted 方法的外部方法的 block , 并打印其他日志,例如- 陷阱——第二次异常,第三次抛出它 陷阱——第三次异常 第四次抛出异常 陷阱——第四次异常 错误是发生了意外的事情 --EXCEPTION

<小时/>
in MyController.m

- (IBAction)fetchResults:(id)sender
{
    NSArray *tableColumnArray = ...............;//some values initialized
    NSArray *identifierArray = ................;//some values initialized
    NSArray *bindVariableArray = ................;//some values initialized
    TableController *pTC = [[TableController alloc] init];
    @try
    {
        [pTC populateData :tableColumnArray :identifierArray :bindVariableArray];// calling populate DataForMD method defined in  TableController class
        [pTC release];
    }
    @catch (NSException * e)
    {
        NSLog(@"Gotcha--4th time exception");
        //want to handle the exception here 
        NSString* theError=[e reason];
        NSLog(@"the error is %@",theError);
    }
}
<小时/>

在TableController.m

-(void)populateData:(NSArray *)tableColumnArray:(NSArray *)identifierArray:(NSArray *)bindVariableArray
{
    [self setTableColumnArray:tableColumnArray];
    [self setColumnIdentifierArray:identifierArray];
    [self setBindVarArray:bindVariableArray];
    @try
    {
        NSLog(@"m here 1");
        [self initializeTest];// calling initializeTest method 
    }
    @catch (NSException * e)
    {
        //Do not want to handle it here 
        NSLog(@"Gotcha--3rd time exception");
        NSLog(@"Throwing exception for the 4th time");
        @throw e;
    }
}

-(void)initializeTest
{
    @try
    {
        ISTQuery* theQuery = (ISTQuery*)[ISTQueryGenerator getQueryByName:[self queryClassName]];
        ..........
        ...........//some loc here
        [theQuery run];
        .................//some loc here
        if(theQuery)
        {
            //Calling FinderCompleted method 
            //supposed to get error here
            [[self modelFinder] startWithRecipient:self andNotificationSelector:@selector(FinderCompleted:)];
        }
    }
    @catch(NSException *e) 
    {
        NSLog(@"Gotcha--2st time exception, throwing it 3rd time");
        //Do not want to handle it here 
        @throw e; // rethrows e implicitly
    }
}

- (void)FinderCompleted:(id)args
{
    @try
    {   //getting some error while back-end transaction 
        NSString* theError = [ISTModelFinder errorMessageFromFinderArgs:args];
        if (theError)
        {
            NSLog(@"Inside the error");
            NSLog(@"You got nothing in ur bucket :D");
            NSException *e = [NSException
                              exceptionWithName:@"InternalErrorException"
                              reason:@"Something unexpected happened --EXCEPTION"
                              userInfo:nil];
            NSLog(@"Throwing exception for the 1st time");
            @throw e;
        }       
        else
        {
            //do sth else
        }
    }   
    @catch(NSException *e) 
    {
        NSLog(@"Gotcha--1st time exception , throwing it 2nd time");
        NSString* theError=[e reason];
        //Do not want to handle it here 
        NSLog(@"the error is == %@",theError);
        @throw e; // rethrows e implicitly
    }
}   

最佳答案

您不能在 Cocoa 或 iOS 编程中使用异常进行流程控制。异常纯粹是为了识别不可恢复的错误,通常,程序会在不久之后故意崩溃。 (此规则有少数异常(exception),其中大多数都有针对它们提交的错误,以弃用并消除相关 API。)

使用NSError模式来管理用户可恢复的错误。

目前尚不清楚您的代码为何无法运行。但这看起来不像真正的代码。你尝试过什么?

<小时/>

I had added the code snippet, please suggest how to achieve that functionality.

您要做的第一件事是不要使用异常

使用 NSError 模式。为此,您需要重构代码。这是值得的。

请参阅 error handling in cocoa 上的文档.

关于objective-c - cocoa 中 try/catch 内部的错误传播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11846899/

相关文章:

ios - 使用 GCDWebServer 将 UIImage 发送到 LocalwebServer

objective-c - 如何将 HIDAPI 库添加到现有的 Mac 项目

objective-c - 从 NSData 创建多个 NSDocument 的最简洁方法

Ruby "defined?"运算符工作错误?

c++ - 字符串超出范围异常

ios - 无法将形状层添加到 View 的层中

objective-c - 三个 Objective-C 构造函数问题

cocoa - 如何使用显式 NSLayoutManager、NSTextStorage、NSTextContainer 以编程方式设置 NSTextView?

objective-c - 将 C 函数设置为 NSButton 的选择器不会产生任何结果

Java 在特定调用上禁用堆栈跟踪?