日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发之----异常处理

發(fā)布時(shí)間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发之----异常处理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。