zoukankan      html  css  js  c++  java
  • NSException异常处理

    异常处理是管理非典型事件(例如未被识别的消息)的过程,此过程将会中断正常的程序执行。如果没有足够的错误处理,遇到非典型事件时,程序可能立刻抛出(或者引发)一种被称之为异常的东西,然后结束运行。

    异常的类型

    程序抛出异常的原因多种多样,可由硬件导致也可由软件引起。异常的例子很多,包括被零除、下溢和上异之类的数学错误,调用未定义的指令(例如,试图调用一个没有定义的方法 )以及试图越界访问群体中的元素 。

    Cocoa异常由NSException对象作为载体,下面是NSException的声明:

    复制代码
    1 @interface NSException : NSObject <NSCopying, NSCoding> {
    2     @private
    3     NSString        *name;
    4     NSString        *reason;
    5     NSDictionary    *userInfo;
    6     id            reserved;
    7 }
    复制代码
    • name — a short string that is used to uniquely identify the exception. The name is required.

    • reason — a longer string that contains a “human-readable” reason for the exception. The reason is required.

    • An optional dictionary (userInfo) used to supply application-specific data to the exception handler. For example, if the return value of a method causes an exception to be raised, you could pass the return value to the exception handler through userInfo.

    你可以在异常捕获后提取有用的信息,还可以适当的弹出一个异常警告框。

    抛出的异常必须是NSException或者其子类,不能是其他类。

    下面提供示例代码:

    复制代码
     1     NSException* ex = [[NSException alloc] initWithName:@"ExceptionName"   // just for test
     2                                                  reason:@"XXX"
     3                                               userInfo:nil];
     4     
     5     CustomNSException* ex = [[CustomNSException alloc] initWithName:@"CustomNSExceptionName"   // just for test
     6                                                  reason:@"XXX"
     7                                                userInfo:nil];
     8     
     9     @try {
    10         bool error = YES;
    11         if (error) {
    12             @throw ex;
    13         }
    14     }
    15     
    16     @catch ( CustomNSException *exception ) {
    17         NSLog(@"CustomNSException.name = %@" , CustomNSException.name);
    18         NSLog(@"CustomNSException.reason = %@" , CustomNSException.reason);
    19         
    20         // 弹出警告框,提示异常信息
    21         UIAlertView* alert = [[UIAlertView alloc] initWithTitle:CustomNSException.name
    22                                                         message:CustomNSException.reason
    23                                                        delegate:nil
    24                                               cancelButtonTitle:nil
    25                                               otherButtonTitles:nil];
    26         
    27         [alert show];
    28         [alert release];
    29     }
    30     
    31     @catch ( NSException *exception ) {
    32         NSLog(@"exception.name = %@" , exception.name);
    33         NSLog(@"exception.reason = %@" , exception.reason);
    34     }
    35     
    36     @finally {
    37         NSLog(@"@finally");
    38     }
    复制代码
  • 相关阅读:
    redis info详解
    redis数据类型-有序集合
    redis数据类型-集合类型
    redis数据类型-列表类型
    python——井字小游戏
    python——元组和字典学习笔记
    docker学习笔记
    中型公司网络架构拓扑与详解
    python——将图片转换为字符编码(linux版的)
    python——冒泡排序练习
  • 原文地址:https://www.cnblogs.com/fuland/p/3668004.html
Copyright © 2011-2022 走看看