转载自:http://blog.sina.com.cn/s/blog_71715bf8010166qf.html

开篇大话:

Object-C语言的异常处理符号和C++、JAVA相似。再加上使用NSException,NSError或者自定义的类,你可以在你的应用程序里添加强大的错误处理机制。异常处理机制是由这个四个关键字支持的:@try,@catch,@thorw,@finally。当代码有可能出现异常时,我们把他放到@try语句块中。@catch()块包含了处理@try块里的抛出的异常的逻辑。无论异常是否发生,@finally块里面的语句都会执行。如果直接使用@throw块来抛出异常,这个异常本质上是一个OC的对象。咱们可以使用NSException对象,但是不局限于他们。

Objective-C的异常比较像Java的异常处理,也有@finally的处理,不管异常是否捕获都都要执行。

异常处理捕获的语法:

  • @try {
  • <#statements#>
  • }
  • @catch (NSException *exception) {
  • <#handler#>
  • }
  • @finally {
  • <#statements#>
  • }

@catch{} 块 对异常的捕获应该先细后粗,即是说先捕获特定的异常,再使用一些泛些的异常类型。

我们自定义两个异常类,看看异常异常处理的使用。

1、新建SomethingException,SomeOverException这两个类,都继承与NSException类。

SomethingException.h

  • #import <Foundation/Foundation.h>
  • @interface SomethingException : NSException
  • @end

SomethingException.m

  • #import "SomethingException.h"
  • @implementation SomethingException
  • @end

SomeOverException.h

  • #import <Foundation/Foundation.h>
  • @interface SomeOverException : NSException
  • @end

SomeOverException.m

  • #import "SomeOverException.h"
  • @implementation SomeOverException
  • @end

2、新建Box类,在某些条件下产生异常。

  • #import <Foundation/Foundation.h>
  • @interface Box : NSObject
  • {
  • NSInteger number;
  • }
  • -(void) setNumber: (NSInteger) num;
  • -(void) pushIn;
  • -(void) pullOut;
  • -(void) printNumber;
  • @end
  • @implementation Box
  • -(id) init {
  • self = [super init];
  • if ( self ) {
  • [self setNumber: 0];
  • }
  • return self;
  • }
  • -(void) setNumber: (NSInteger) num {
  • number = num;
  • if ( number > 10 ) {
  • NSException *e = [SomeOverException
  • exceptionWithName: @"BoxOverflowException"
  • reason: @"The level is above 100"
  • userInfo: nil];
  • @throw e;
  • else if ( number >= 6 ) {
  • // throw warning
  • NSException *e = [SomethingException
  • exceptionWithName: @"BoxWarningException"
  • reason: @"The level is above or at 60"
  • userInfo: nil];
  • @throw e;
  • else if ( number < 0 ) {
  • // throw exception
  • NSException *e = [NSException
  • exceptionWithName: @"BoxUnderflowException"
  • reason: @"The level is below 0"
  • userInfo: nil];
  • @throw e;
  • }
  • }
  • -(void) pushIn {
  • [self setNumber: number + 1];
  • }
  • -(void) pullOut {
  • [self setNumber: number - 1];
  • }
  • -(void) printNumber {
  • NSLog(@"Box number is: %d", number);
  • }
  • @end

这个类的作用是,初始化Box时,number数字是0,可以用pushIn 方法往Box里推入数字,每调用一次,number加1.当number数字大于等于6时产生SomethingException异常,告诉你数字达到或超过6了,超过10时产生SomeOverException异常,小于1时产生普通的NSException异常。

这里写 [SomeOverException  exceptionWithName:@"BoxOverflowException"  reason:@"The level is above 100"异常的名称和理由,在捕获时可以获取。

3、使用Box,在适当添加下捕获Box类的异常

3.1、在没超过6时,没有异常

  • - (void)viewDidLoad
  • {
  • [super viewDidLoad];
  • NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  • Box *box = [[Box alloc]init];
  • for (int i = 0; i < 5; i++) {
  • [box pushIn];
  • [box printNumber];
  • }
  • }

打印结果:

Box number is: 1

Box number is: 2

Box number is: 3

Box number is: 4

Box number is: 5

3.2 超过6,产生异常

  • for (int i = 0; i < 11; i++) {
  • [box pushIn];
  • [box printNumber];
  • }
  • 2012-07-04 09:12:05.889 ObjectiveCTest[648:f803] Box number is: 1
  • 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 2
  • 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 3
  • 2012-07-04 09:12:05.890 ObjectiveCTest[648:f803] Box number is: 4
  • 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] Box number is: 5
  • 2012-07-04 09:12:05.891 ObjectiveCTest[648:f803] *** Terminating app due to uncaught exception 'BoxWarningException', reason: 'The number is above or at 60'

这是时,程序抛出异常崩溃了。那怎么使程序不崩溃呢,做异常处理。

3.3、加上异常处理

  • for (int i = 0; i < 11; i++) {
  • @try {
  • [box pushIn];
  • }
  • @catch (SomethingException *exception) {
  • NSLog(@"%@ %@", [exception name], [exception reason]);
  • }
  • @catch (SomeOverException *exception) {
  • NSLog(@"%@", [exception name]);
  • }
  • @finally {
  • [box printNumber];
  • }
  • }

运行,程序没有崩溃,打印结果:

  • 2012-07-04 09:14:35.165 ObjectiveCTest[688:f803] Box number is: 1
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 2
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 3
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 4
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] Box number is: 5
  • 2012-07-04 09:14:35.167 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 6
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 7
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] Box number is: 8
  • 2012-07-04 09:14:35.168 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 9
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxWarningException The number is above or at 60
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] Box number is: 10
  • 2012-07-04 09:14:35.169 ObjectiveCTest[688:f803] BoxOverflowException
  • 2012-07-04 09:14:35.225 ObjectiveCTest[688:f803] Box number is: 11

超过10时,SomeOverException异常抛出。

3.4 、小于0时的异常

在Box类的setNumber里,当number小于0时,我们抛出普通异常。

  • @try {
  • [box setNumber:-10];
  • }
  • @catch (NSException *exception) {
  • NSLog(@"%@",[exception name]);
  • }
  • @finally {
  • [box printNumber];
  • }

打印结果:

  • 2012-07-04 09:17:42.405 ObjectiveCTest[753:f803] BoxUnderflowException
  • 2012-07-04 09:17:42.406 ObjectiveCTest[753:f803] Box number is: -10

转载于:https://www.cnblogs.com/nilfor-sun123/p/3602195.html

IOS开发之----异常处理相关推荐

  1. 【iOS 开发】Objective-C 运算符

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41624613 参考文章 : 1.[iOS 开发]Object-C 运算符 2.[iO ...

  2. iOS开发-多线程开发之线程安全篇

    前言:一块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源,比如多个线程访问同一个对象.同一个变量.同一个文件和同一个方法等.因此当多个线程访问同一块资源时,很容易会发生数据错误及数据不安 ...

  3. iOS开发面试知识整理 – OC基础 (二)

    iOS | 面试知识整理 – OC基础 (二) 1.C和 OC 如何混编 xcode可以识别一下几种扩展名文件: .m文件,可以编写 OC语言 和 C 语言代码 .cpp: 只能识别C++ 或者C语言 ...

  4. iOS开发笔记 - 网络篇

    计算机网络基础   计算机网络是多台独立自主的计算机互联而成的系统的总称,最初建立计算机网络的目的是实现信息传递和资源共享.   如果说计算机是第二次世界大战的产物,那么计算机网络则是美苏冷战的产物. ...

  5. 美团App iOS开发与FRP

    版权说明 本文为刊登于<程序员>杂志2016年5月刊.如需转载,请与<程序员>杂志联系. 背景和面临的问题 随着移动互联网的蓬勃发展,iOS App的复杂度呈指数增长.美团·大 ...

  6. Delphi for iOS开发指南(14):在iOS应用程序中使用InterBase ToGo

    在开始这篇教程之前,你应该预先阅读并按下面教程实际操作过: •iOS开发指南(9):在iOS应用程序中使用ListBox组件来显示TableView 这篇教程描述了在iOS Device上,通过dbE ...

  7. IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题

    在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的:    这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需 ...

  8. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇-transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  9. 【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示

    [ios开发/Xcode]使用UITableView完成学生信息及成绩的显示 设计思想 实现效果 源代码 设计思想 首先创建所有页面的故事版,包括,登录.注册与成绩页面 接着设置故事版的关联代码,如下 ...

最新文章

  1. 为什么我抛弃了 Ubuntu?
  2. HDU - 4416 Good Article Good sentence(广义后缀自动机/后缀自动机)
  3. word表格图片自动适应表格大小_几招教你快速解决word文字、图片、表格排版问题,你肯定遇到过...
  4. java类型转换造成的字节丢失,java 编码转换(已解决,转换字节丢失,无法实现)
  5. java 类持久化_Java 持久化之 -- IO 全面整理(看了绝不后悔)
  6. Java DataOutputStream writeChars()方法及示例
  7. Qt|C/C++植物大战僵尸修改器(用户版)
  8. 2019年的流水账和总结
  9. 千锋培训php怎么样,零基础学员真实感受 选择千锋PHP培训完成人生蜕变
  10. gif透明背景动画_ThunderSoft GIF Converter(GIF转换器)中文版分享
  11. sxe增加服务器,sXe Injected 反作弊插件
  12. c语言上机试题8,计算机考试二级C语言上机试题下[8]
  13. netlimiter 4 功能使用TCP UDP上行下行流量监控
  14. unity Layer CullingMask
  15. android开发中磁场传感器,Android NDK中的陀螺仪和磁场传感器事件
  16. 芒课 —— 2464试题
  17. Dwr概述与入门讲解
  18. oracle获取字符串最后一个逗号后面的字符
  19. 水仙花数(输出全部水仙花数)
  20. HTML在线编辑器(HTML+js)

热门文章

  1. Android 中文 API (93) —— BaseExpandableListAdapter
  2. AVL树C++实现(插入,删除,查找,清空,遍历操作)
  3. 20172307 结对编程项目-四则运算 第二周 阶段总结
  4. python视频教程大全
  5. Openstack启用spice协议
  6. redhat6.4中手动创建oracle11g数据库
  7. 空腹吃香蕉对身体好吗?哪些水果不宜空腹吃
  8. 五种方法查看Ubuntu/Redhat等Linux系统版本号等系统信息
  9. 基于高德地图Windows Phone API 快速开发地图相关APP(二)
  10. Bluetooth4.0