iOS捕獲異常,常用的異常處理方法

來源:互聯網
上載者:User

標籤:

本文轉載至 http://www.cocoachina.com/ios/20141229/10787.html 

前言:在開發APP時,我們通常都會需要捕獲異常,防止應用程式突然的崩潰,防止給予使用者不友好的體驗。其實Objective-C的異常處理方法和JAVA的雷同,懂JAVA的朋友一看就懂。我為什麼要寫這篇博文呢?因為我發現百度上的介紹方法,很多都不是我想要的,而我想要的又說得不清楚,重點是大家都是直接複製別人的代碼。。。於是不多說,大家往下看~~~

以下程式已測試並通過:

裝置:iOS 8模擬器中

開發工具:XCode6.1

使用@try、catch捕獲異常:

以下是最簡單的代碼寫法,其中@finally可以去掉:

123456789 @try {    // 可能會出現崩潰的代碼}@catch (NSException *exception) {    // 捕獲到的異常exception}@finally {    // 結果處理}

在這裡舉多一具比較詳細的方法,拋出異常:

12345678910111213141516 @try {    // 1    [self tryTwo];}@catch (NSException *exception) {    // 2    NSLog(@"%s\n%@", __FUNCTION__, exception);//        @throw exception; // 這裡不能再拋異常}@finally {    // 3    NSLog(@"我一定會執行");}// 4// 這裡一定會執行NSLog(@"try");

tryTwo方法代碼:

12345678910111213141516171819202122 - (void)tryTwo{    @try {        // 5        NSString *str = @"abc";        [str substringFromIndex:111]; // 程式到這裡會崩    }    @catch (NSException *exception) {        // 6//        @throw exception; // 拋出異常,即由上一級處理        // 7        NSLog(@"%s\n%@", __FUNCTION__, exception);    }    @finally {        // 8        NSLog(@"tryTwo - 我一定會執行");    }         // 9    // 如果拋出異常,那麼這段代碼則不會執行    NSLog(@"如果這裡拋出異常,那麼這段代碼則不會執行");}

為了方便大家理解,我在這裡再說明一下情況:
如果6拋出異常,那麼執行順序為:1->5->6->8->3->4
如果6沒拋出異常,那麼執行順序為:1->5->7->8->9->3->4

2)部分情況的崩潰我們是無法避免的,就算是QQ也會有崩潰的時候。因此我們可以在程式崩潰之前做一些“動作”(收集錯誤資訊),以下例子是把捕獲到的異常發送至開發人員的郵箱。

AppDelegate.m

1234567891011121314151617181920212223242526 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);    return YES;} void UncaughtExceptionHandler(NSException *exception) {    /**     *  擷取異常崩潰資訊     */    NSArray *callStack = [exception callStackSymbols];    NSString *reason = [exception reason];    NSString *name = [exception name];    NSString *content = [NSString stringWithFormat:@"========異常錯誤報表========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[callStack componentsJoinedByString:@"\n"]];     /**     *  把異常崩潰資訊發送至開發人員郵件     */    NSMutableString *mailUrl = [NSMutableString string];    [mailUrl appendString:@"mailto:[email protected]"];    [mailUrl appendString:@"?subject=程式異常崩潰,請配合發送異常報告,謝謝合作!"];    [mailUrl appendFormat:@"&body=%@", content];    // 開啟地址    NSString *mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailPath]];

iOS捕獲異常,常用的異常處理方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.