iOS - OC 异常处理

1、@try 语句

    @try {
    
        // Code that can potentially throw an exception  可能会抛出异常的代码块
    
        statement
        . . .
    
    } @catch (NSException *exception) {
    
        // Handle an exception thrown in the @try block  处理 @try 块抛出的异常
    
        NSLog(@"%@, %@", [exception name], [exception reason]);
    
        statement
        . . .
    
    } @finally {
    
        // Code that gets executed whether or not an exception is thrown  无论是否抛出异常都要被执行的代码块
    
        statement
        . . .   
    }
  • 当出现异常时,@catch 块被执行。包涵异常信息的 NSException 对象作为参数传递给这个块。name 监测异常的名称,reason 方法给出原因(运行时系统还是会将原因自动输出)。
  • 在 @catch 块中的最后一条语句执行后,程序会立即执行之后的语句,程序并不会终止运行。

  • 在 iOS 开发时很少使用 try catch 错误处理机制,因为 iOS ARC 开发时,所有 retain, release, autorelease 都是编译自动增加的,一旦使用了 try catch,程序如果出现异常,系统没有办法准确的判断第几个语句出现问题,导致 retain, release 失衡,会造成内存泄漏。

转载于:https://www.cnblogs.com/QianChia/p/5780694.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值