使いたいです+[NSException exceptionWithName:reason:userInfo:]
。
しかし、引数にはどの文字列を使用すればよいName:
でしょうか?
例外名はプロジェクト内で一意にする必要がありますか?
または、すべての例外に @"MyException" を使用できますか?
そして、どの例外名が使用されているのかわかりません。
例外名はどこで使用されますか?
使いたいです+[NSException exceptionWithName:reason:userInfo:]
。
しかし、引数にはどの文字列を使用すればよいName:
でしょうか?
例外名はプロジェクト内で一意にする必要がありますか?
または、すべての例外に @"MyException" を使用できますか?
そして、どの例外名が使用されているのかわかりません。
例外名はどこで使用されますか?
の名前を使用できます@catch (NSException *theErr)
。
@catch (NSException *theErr)
{
tst_name = [theErr name];
if ([tst_name isEqualToString:@"name"])
}
引数 Name: にはどの文字列を使用すればよいですか?
意味のあるもの。
例外名はプロジェクト内で一意にする必要がありますか?
いいえ。
または、すべての例外に @"MyException" を使用できますか?
はい。ただし、意味のある名前を使用する必要があります。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSNumber *tst_dividend, *tst_divisor, *tst_quotient;
// prepare the trap
@try
{
// initialize the following locals
tst_dividend = [NSNumber numberWithLong:8];
tst_divisor = [NSNumber numberWithLong:0];
// attempt a division operation
tst_quotient = [self divideLong:tst_dividend by:tst_divisor];
// display the results
NSLog (@"The answer is: %@", tst_quotient);
}
@catch (NSException *theErr)
{
// an exception has occured
// display the results
NSLog (@"The exception is:\n name: %@\nreason: %@"
, [theErr name], [theErr reason]);
}
@finally
{
//...
// the housekeeping domain
//...
}
}
- (NSNumber *)divideLong:(NSNumber *)aDividend by:(NSNumber *)aDivisor
{
NSException *loc_err;
long loc_long;
// validity check
loc_long = [aDivisor longValue];
if (loc_long == 0)
{
// create and send an exception signal
loc_err = [NSException
exceptionWithName:NSInvalidArgumentException
reason:@"Division by zero attempted"
userInfo:nil];
[loc_err raise]; //locate nearest exception handler,
//If the instance fails to locate a handler, it goes straight to the default exception handler.
}
else
// perform the division
loc_long = [aDividend longValue] / loc_long;
// return the results
return ([NSNumber numberWithLong:loc_long]);
}
最終的に、この方法で例外を追加する目的は、問題をできるだけ早く検出し、報告して、診断できるようにすることです。
そのため、プロジェクトに固有の例外名を選択するか、問題 (つまり、ソース行、メソッド) に固有の例外名を選択するかは、どちらが最良の診断情報を提供するかに依存します。
例外名はアプリ間で共有できます。これは、例外の発生元を特定するためにアプリによって報告されるためです。