`
啸笑天
  • 浏览: 3433463 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

Objective-C - 异常处理(NSException)

 
阅读更多

苹果关于异常的详细文档:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Exceptions/Exceptions.html#//apple_ref/doc/uid/10000012i

 

 

关于自定义异常或者扩展:

Objective-C中处理异常是依赖于NSException实现的,它是异常处理的基类,它是一个实体类,而并非一个抽象类,所以你可以直接使用它或者继承它扩展使用:

1.直接使用,分两种,抛出默认的异常,和自定义自己的新的种类的异常:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        NSException* ex = [[NSException alloc]initWithName:@"MyException"  
                                                    reason:@"b==0"   
                                                  userInfo:nil];  
        
        @try   
        {  
            int b = 0;   
            switch (b)    
            {  
                case 0:  
                    @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];  
        
    }
    return 0;
}

ps:

Initializes and returns a newly allocated exception object.

- (id)initWithName:(NSString *)name reason:(NSString *)reason userInfo:(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 created NSException object or nil if the object couldn't be created.

Discussion

This is the designated initializer.

Availability
  • Available in iOS 2.0 and later.


2.扩展使用,这个推荐你需要自定义一些功能的时候使用,比如,当捕获到指定的异常的时候弹出警告框之类的:
@interface MyException : NSException    
-(void)popAlert  
@end

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

 使用:

- (IBAction)btnClicked_Exception:(id)sender   
{  
    MyException* ex = [[MyException alloc]initWithName:@"MyException"  
                                                reason:@"除数为0了!"   
                                              userInfo:nil];  
      
     
    @try   
    {  
        int b = 0;   
        switch (b)    
        {  
            case 0:  
                @throw(ex);//b=0,则抛出异常;  
                break;  
            default:  
                break;  
        }  
    }  
  
  
    @catch (MyException *exception)//捕获抛出的异常   
    {  
  
      [exception popAlert];  
  
      NSLog(@"b==0 Exception!");   
    }  
   
    @finally   
    {   
      NSLog(@"finally!");   
    }   
  
    [ex release];  
}  
 这个时候,捕获到异常,它就会弹出警告框了。当然,你还可以在MyException里面加一些指定的异常的通用处理方法。

只要你愿意,你就可以随意的定制它!

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    KSCrash:终极iOS崩溃报告器

    崩溃 最终崩溃记者 另一个坠机记者? 为什么?... 内省寄存器和堆栈中的对象(C字符串和Objective-C对象,包括ivars)。 提取有关异常引用的对象的信息(例如“发送到实例0xa26d9a0的无法识别的选择

    NSExceptionSwift

    一个很小的库,可让您直接在Swift代码中捕获Objective-C NSException。 一体化 可可豆 您可以使用安装NSExceptionSwift将其添加到您的Podfile : platform :ios , '8.0' use_frameworks! target 'MyApp' do pod '...

    ios Crash异常捕捉demo

    主要用于NSException 异常的捕捉,可以捕获对应的异常。

    Catfish:[已弃用] Catfish 是一组对 iOS 开发人员有用的库和类别,使开发移动应用程序更容易

    鲶鱼它是一个开源项目,包含一组用于 iOS 平台的库,用 Objective-C 编写。 它的目标是帮助程序员拥有大多数项目中日常所需的通用功能,使开发移动应用程序变得更容易。 基本上,这是重构我参与过的项目的结果。 ...

    SDStatisticsSDK

    SD统计SDK采集应用程序的崩溃信息,主要分为以下两种场景: NSException异常Unix信号异常捕获NSException异常通过NSSetUncaughtExceptionHandler函数来设置异常处理函数,然后收集异常变量信息捕获信号Mach异常和...

    IOS开发中的各种Category

    NSException NSFileManager NSObject NSSet NSString NSTimer NSURL UIKit UIBezierPath UIButton UIColor UIDevice UIImage UIImageView UILable UINavigationController UIResponder UIScrollView UISearchBar ...

    ios 录音文件caf转mp3

    注意音频参数的设置,如果声音异常,请调整参数。 code: AVAudioSession *session = [AVAudioSession sharedInstance]; NSError *sessionError; [session setCategory:AVAudioSessionCategoryPlayAndRecord ...

Global site tag (gtag.js) - Google Analytics