IOS开发之----异常处理
轉載自: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开发之----异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows多线程同步--临界区
- 下一篇: Matlab标识指令中字符的精细控制