IOS开发之----异常处理
轉(zhuǎn)載自:http://blog.sina.com.cn/s/blog_71715bf8010166qf.html
開(kāi)篇大話:
Object-C語(yǔ)言的異常處理符號(hào)和C++、JAVA相似。再加上使用NSException,NSError或者自定義的類,你可以在你的應(yīng)用程序里添加強(qiáng)大的錯(cuò)誤處理機(jī)制。異常處理機(jī)制是由這個(gè)四個(gè)關(guān)鍵字支持的:@try,@catch,@thorw,@finally。當(dāng)代碼有可能出現(xiàn)異常時(shí),我們把他放到@try語(yǔ)句塊中。@catch()塊包含了處理@try塊里的拋出的異常的邏輯。無(wú)論異常是否發(fā)生,@finally塊里面的語(yǔ)句都會(huì)執(zhí)行。如果直接使用@throw塊來(lái)拋出異常,這個(gè)異常本質(zhì)上是一個(gè)OC的對(duì)象。咱們可以使用NSException對(duì)象,但是不局限于他們。
?
Objective-C的異常比較像Java的異常處理,也有@finally的處理,不管異常是否捕獲都都要執(zhí)行。
異常處理捕獲的語(yǔ)法:
?
- @try?{??
- ??????<#statements#>??
- ??}??
- ??@catch?(NSException?*exception)?{??
- ??????<#handler#>??
- ??}??
- ??@finally?{??
- ??????<#statements#>??
- ??}??
?@catch{} 塊 對(duì)異常的捕獲應(yīng)該先細(xì)后粗,即是說(shuō)先捕獲特定的異常,再使用一些泛些的異常類型。
我們自定義兩個(gè)異常類,看看異常異常處理的使用。
1、新建SomethingException,SomeOverException這兩個(gè)類,都繼承與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類,在某些條件下產(chǎn)生異常。
?
- #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??
這個(gè)類的作用是,初始化Box時(shí),number數(shù)字是0,可以用pushIn 方法往Box里推入數(shù)字,每調(diào)用一次,number加1.當(dāng)number數(shù)字大于等于6時(shí)產(chǎn)生SomethingException異常,告訴你數(shù)字達(dá)到或超過(guò)6了,超過(guò)10時(shí)產(chǎn)生SomeOverException異常,小于1時(shí)產(chǎn)生普通的NSException異常。
這里寫(xiě) [SomeOverException??exceptionWithName:@"BoxOverflowException" ?reason:@"The level is above 100"異常的名稱和理由,在捕獲時(shí)可以獲取。
3、使用Box,在適當(dāng)添加下捕獲Box類的異常
3.1、在沒(méi)超過(guò)6時(shí),沒(méi)有異常
?
- -?(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];??
- ????}??
- }??
打印結(jié)果:
Box number is: 1
Box number is: 2
Box number is: 3
Box number is: 4
Box number is: 5
3.2 超過(guò)6,產(chǎn)生異常
?
- 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'??
這是時(shí),程序拋出異常崩潰了。那怎么使程序不崩潰呢,做異常處理。
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];??
- ????????}??
- ????}??
運(yùn)行,程序沒(méi)有崩潰,打印結(jié)果:
?
- 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??
超過(guò)10時(shí),SomeOverException異常拋出。
3.4 、小于0時(shí)的異常
在Box類的setNumber里,當(dāng)number小于0時(shí),我們拋出普通異常。
?
- @try?{??
- ??????[box?setNumber:-10];??
- ??}??
- ??@catch?(NSException?*exception)?{??
- ??????NSLog(@"%@",[exception?name]);??
- ??}??
- ??@finally?{??
- ??????[box?printNumber];??
- ??}??
打印結(jié)果:
?
- 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 ?
轉(zhuǎn)載于:https://www.cnblogs.com/nilfor-sun123/p/3602195.html
總結(jié)
以上是生活随笔為你收集整理的IOS开发之----异常处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: windows多线程同步--临界区
- 下一篇: Matlab标识指令中字符的精细控制