zoukankan      html  css  js  c++  java
  • 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 块中的最后一条语句执行后,程序会立即执行之后的语句,程序并不会终止运行。

    2、注意

    • 在 iOS 开发时很少使用 try catch 错误处理机制,因为 iOS ARC 开发时,所有 retain, release, autorelease 都是编译自动增加的,
    • 一旦使用了 try catch,程序如果出现异常,系统没有办法准确的判断第几个语句出现问题,导致 retain, release 失衡,会造成内存泄漏。
  • 相关阅读:
    STL之vector
    [洛谷P3942] 将军令
    [洛谷P2127] 序列排序
    [USACO07FEB]新牛棚Building A New Barn
    [洛谷P1120] 小木棍 [数据加强版]
    [洛谷P1438] 无聊的数列
    我的Emacs配置
    [CQOI2015]任务查询系统
    可持久化数组入门
    学习openstack(六)
  • 原文地址:https://www.cnblogs.com/CH520/p/9510675.html
Copyright © 2011-2022 走看看