Objective-C - 异常处理(NSException)
2016-06-15 10:12
363 查看
苹果关于异常的详细文档: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Exceptions.html#//apple_ref/doc/uid/10000012i
关于自定义异常或者扩展:
Objective-C中处理异常是依赖于NSException实现的,它是异常处理的基类,它是一个实体类,而并非一个抽象类,所以你可以直接使用它或者继承它扩展使用:
1.直接使用,分两种,抛出默认的异常,和自定义自己的新的种类的异常:
C代码
1 #import <Foundation/Foundation.h>
2
3 int main (int argc, const char * argv[])
4 {
5
6 @autoreleasepool {
7 NSException* ex = [[NSException alloc]initWithName:@"MyException"
8 reason:@"b==0"
9 userInfo:nil];
10
11 @try
12 {
13 int b = 0;
14 switch (b)
15 {
16 case 0:
17 @throw(ex);//b=0,则抛出异常;
18 break;
19 default:
20 break;
21 }
22 }
23 @catch (NSException *exception)//捕获抛出的异常
24 {
25 NSLog(@"exception.name= %@" ,exception.name);
26 NSLog(@"exception.reason= %@" ,exception.reason);
27 NSLog(@"b==0 Exception!");
28 }
29 @finally
30 {
31 NSLog(@"finally!");
32 }
33 [ex release];
34
35 }
36 return 0;
37 }
ps:
Initializesand returns a newly allocated exception object.
- (id)initWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo
Parameters
name
Thename of the exception.
reason
Ahuman-readable message string summarizing the reason for the exception.
userInfo
Adictionary containing user-defined information relating to the exception
ReturnValue
Thecreated NSException object or nil if the object couldn't be created.
Discussion
Thisis the designated initializer.
Availability
• Available in iOS 2.0 and later.
2.扩展使用,这个推荐你需要自定义一些功能的时候使用,比如,当捕获到指定的异常的时候弹出警告框之类的:
C代码
1 @interface MyException : NSException
2 -(void)popAlert
3 @end
Java代码
1 @implementation MyException
2
3 - (void)popAlert
4 {
5 //弹出报告异常原因的警告框 reason
6 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Tips"
7 message:self.reason
8 delegate:nil
9 cancelButtonTitle:nil
10 otherButtonTitles:nil];
11
12 [alert show];
13 [alert release];
14 }
15 @end
使用:
C代码
1 - (IBAction)btnClicked_Exception:(id)sender
2 {
3 MyException* ex = [[MyException alloc]initWithName:@"MyException"
4 reason:@"除数为0了!"
5 userInfo:nil];
6
7
8 @try
9 {
10 int b = 0;
11 switch (b)
12 {
13 case 0:
14 @throw(ex);//b=0,则抛出异常;
15 break;
16 default:
17 break;
18 }
19 }
20
21
22 @catch (MyException *exception)//捕获抛出的异常
23 {
24
25 [exception popAlert];
26
27 NSLog(@"b==0 Exception!");
28 }
29
30 @finally
31 {
32 NSLog(@"finally!");
33 }
34
35 [ex release];
36 }
这个时候,捕获到异常,它就会弹出警告框了。当然,你还可以在MyException里面加一些指定的异常的通用处理方法。
只要你愿意,你就可以随意的定制它!
关于自定义异常或者扩展:
Objective-C中处理异常是依赖于NSException实现的,它是异常处理的基类,它是一个实体类,而并非一个抽象类,所以你可以直接使用它或者继承它扩展使用:
1.直接使用,分两种,抛出默认的异常,和自定义自己的新的种类的异常:
C代码
1 #import <Foundation/Foundation.h>
2
3 int main (int argc, const char * argv[])
4 {
5
6 @autoreleasepool {
7 NSException* ex = [[NSException alloc]initWithName:@"MyException"
8 reason:@"b==0"
9 userInfo:nil];
10
11 @try
12 {
13 int b = 0;
14 switch (b)
15 {
16 case 0:
17 @throw(ex);//b=0,则抛出异常;
18 break;
19 default:
20 break;
21 }
22 }
23 @catch (NSException *exception)//捕获抛出的异常
24 {
25 NSLog(@"exception.name= %@" ,exception.name);
26 NSLog(@"exception.reason= %@" ,exception.reason);
27 NSLog(@"b==0 Exception!");
28 }
29 @finally
30 {
31 NSLog(@"finally!");
32 }
33 [ex release];
34
35 }
36 return 0;
37 }
ps:
Initializesand returns a newly allocated exception object.
- (id)initWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo
Parameters
name
Thename of the exception.
reason
Ahuman-readable message string summarizing the reason for the exception.
userInfo
Adictionary containing user-defined information relating to the exception
ReturnValue
Thecreated NSException object or nil if the object couldn't be created.
Discussion
Thisis the designated initializer.
Availability
• Available in iOS 2.0 and later.
2.扩展使用,这个推荐你需要自定义一些功能的时候使用,比如,当捕获到指定的异常的时候弹出警告框之类的:
C代码
1 @interface MyException : NSException
2 -(void)popAlert
3 @end
Java代码
1 @implementation MyException
2
3 - (void)popAlert
4 {
5 //弹出报告异常原因的警告框 reason
6 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Tips"
7 message:self.reason
8 delegate:nil
9 cancelButtonTitle:nil
10 otherButtonTitles:nil];
11
12 [alert show];
13 [alert release];
14 }
15 @end
使用:
C代码
1 - (IBAction)btnClicked_Exception:(id)sender
2 {
3 MyException* ex = [[MyException alloc]initWithName:@"MyException"
4 reason:@"除数为0了!"
5 userInfo:nil];
6
7
8 @try
9 {
10 int b = 0;
11 switch (b)
12 {
13 case 0:
14 @throw(ex);//b=0,则抛出异常;
15 break;
16 default:
17 break;
18 }
19 }
20
21
22 @catch (MyException *exception)//捕获抛出的异常
23 {
24
25 [exception popAlert];
26
27 NSLog(@"b==0 Exception!");
28 }
29
30 @finally
31 {
32 NSLog(@"finally!");
33 }
34
35 [ex release];
36 }
这个时候,捕获到异常,它就会弹出警告框了。当然,你还可以在MyException里面加一些指定的异常的通用处理方法。
只要你愿意,你就可以随意的定制它!
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Ruby中的异常处理代码编写示例
- SQL Server 2005 中使用 Try Catch 处理异常
- MySQL抛出Incorrect string value异常分析
- 浅谈C#中简单的异常引发与处理操作
- 详解C#编程中异常的创建和引发以及异常处理
- 详解JavaScript中的异常处理方法
- java程序中的延时加载异常及解决方案
- 解析Java异常的栈轨迹及其相关方法
- .NET(C#):Emit创建异常处理的方法
- windows7服务器上weblogic启动失败异常解决方法
- 有关ajax的error与后台的异常问题解决
- 深入探讨JAVA中的异常与错误处理
- 讲解iOS开发中基本的定位功能实现