我想在方法的 catch 块内引发异常,并在调用早期方法的另一个方法的 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 块永远无法访问。控件永远不会返回到方法 B() 的 catch 块。请建议。
谢谢, 苏丹苏
这是一些代码。我正在从 MyController 调用 TableController 的方法(populateData)。异常应该发生在 TableController(initializeTest) 的另一个方法中,我将它扔到 FinderCompleted 方法的 try 块中。在相同方法的 catch 块中,重新抛出异常,因为我不想在这里处理它。控件仅限于方法的最里面的 catch 块 - (void)FinderCompleted:(id)args NSLog 打印如下 - m here 1 在错误内部你的存储桶中什么都没有:D 第一次抛出异常 Gotcha- -第一次异常,第二次抛出错误是==发生了意外的事情--EXCEPTION
在那之后,我不知道控制在哪里。我希望控件去捕获调用 FinderCompleted 方法的外部方法的块,并打印其他日志,例如 - Gotcha--第 2 次异常,第 3 次抛出 Gotcha--第 3 次异常 第 4 次抛出异常 Gotcha--第 4 次异常错误是发生了意外的事情--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
}
}