Objective-C - 異常處理(NSException)

蘋果關(guān)于異常的詳細文檔
關(guān)于自定義異常或者擴展:
Objective-C中處理異常是依賴于NSException實現(xiàn)的,它是異常處理的基類,它是一個實體類,而并非一個抽象類,所以你可以直接使用它或者繼承它擴展使用:
1.直接使用,分兩種,拋出默認(rèn)的異常,和自定義自己的新的種類的異常:
OC代碼

#import 
intmain (intargc,constchar* argv[])
{
@autoreleasepool {
NSException* ex = [[NSException alloc]initWithName:@"MyException"
reason:@"b==0"
userInfo:nil];
@try
{
intb = 0;
switch(b)
{
case0:
@throw(ex);//b=0,則拋出異常;
break;
default:
break;
}
}
@catch(NSException *exception)//捕獲拋出的異常
{
NSLog(@"exception.name= %@",exception.name);
NSLog(@"exception.reason= %@",exception.reason);
NSLog(@"b==0 Exception!");
}
@finally
{
NSLog(@"finally!");
}
[ex release];
}
return0;
}

ps:
Initializes and returns a newly allocated exception object.

- (id)initWithName:([NSString](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSString)*)*name*reason:([NSString](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSString)*)*reason*userInfo:([NSDictionary](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSDictionary)*)*userInfo*
Parameters
*name*
The name of the exception.
*reason*
A human-readable message string summarizing the reason for the exception.
*userInfo*
A dictionary containing user-defined information relating to the exception
Return Value
The createdNSExceptionobject ornilif the object couldn't be created.
Discussion
This is the designated initializer.
Availability
Available in iOS 2.0 and later.

2.擴展使用,這個推薦你需要自定義一些功能的時候使用,比如,當(dāng)捕獲到指定的異常的時候彈出警告框之類的:
OC代碼

![](http://upload-images.jianshu.io/upload_images/1823354-533e19d2a925a0c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

@interface MyException : NSException
-(void)popAlert
@end
Java代碼
![](http://upload-images.jianshu.io/upload_images/1823354-4160a2c42246f2e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

@implementationMyException
- (void)popAlert
{
//彈出報告異常原因的警告框 reason
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Tips"
message:self.reason
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alert show];
[alert release];
}
@end

使用:
OC代碼

- (IBAction)btnClicked_Exception:(id)sender
{
MyException* ex = [[MyException alloc]initWithName:@"MyException"
reason:@"除數(shù)為0了!"
userInfo:nil];
@try
{
intb = 0;
switch(b)
{
case0:
@throw(ex);//b=0,則拋出異常;
break;
default:
break;
}
}
@catch(MyException *exception)//捕獲拋出的異常
{
[exception popAlert];
NSLog(@"b==0 Exception!");
}
@finally
{
NSLog(@"finally!");
}
[ex release];
}

這個時候,捕獲到異常,它就會彈出警告框了。當(dāng)然,你還可以在MyException里面加一些指定的異常的通用處理方法。
只要你愿意,你就可以隨意的定制它!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,414評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,120評論 25 708
  • 晚上吃飯了一個面包 騎車40分鐘 57公斤 睡覺,加油,堅持
    SPP164810閱讀 140評論 0 0
  • 我總覺得,人與人之間的情分都是有限的,不管是和自己的家人、親人、朋友甚至是伴侶,這種情分都是有限的。所以一想到這個...
    happy藍色記憶2011閱讀 3,808評論 7 2
  • 作者/小紐扣 十年 如果,你愛一個人。 無論,她是在你身邊,還是遠在天邊。無論你是擁有她,還是錯過了她。 請你,一...
    瘋狂稻草人閱讀 1,061評論 6 1

友情鏈接更多精彩內(nèi)容