10 -- 關(guān)于NSError

一個老外的博客:
https://www.bignerdranch.com/blog/error-handling-in-swift-2/
摘要:
A

 NS_DURING
*// Call a dangerous method or function that raises an           exception:*
 [obj **someRiskyMethod**];
NS_HANDLER
NSLog(@"Oh no!");
[anotherObj **makeItRight**];
NS_ENDHANDLER

B

**-** (**void**)someRiskyMethod
{
[NSException **raise**:@"Kablam"
            **format**:@"This method is not implemented yet. Do     not call!"];
}

C

**@try** {
[obj **someRiskyMethod**];
}
**@catch** (SomeClass *****exception) {
*// Handle the error.*
*// Can use the exception object to gather information.*
}
**@catch** (SomeOtherClass *****exception) {
*// ...*
}

D

**@catch** (id allTheRest) {
*// ...*
}
**@finally** {
*// Code that is executed whether an exception is thrown or not.*
*// Use for cleanup.*
}

E

*// A local variable to store an error object if one comes back:*
**var** error: **NSError**?
*// success is a Bool:*
**let** success **=** someString**.****writeToURL**(someURL,
                                atomically: **true**,
                                encoding: **NSUTF8StringEncoding**,
                                error: **&**error)
**if** **!**success {
*// Log information about the error:*
**println**("Error writing to URL: \(error**!**)")
}

  **enum** **AwfulError**: **ErrorType** {
  **case** **Bad**
  **case** **Worse**
  **case** **Terrible**

}

F

func  doDangerousStuff () throws ->SomeObject {
*// If something bad happens throw the error:*
throw  AwfulError Bad

*// If something worse happens, throw another error: *
throw **AwfulError****.****Worse**

*// If something terrible happens, you know what to do: *
throw **AwfulError****.****Terrible**

*// If you made it here, you can return:*
**return** **SomeObject**()
}

G

**do** {
**let** theResult **=** try obj**.****doDangerousStuff**()
}
catch **AwfulError****.****Bad** {
*// Deal with badness.*
}
catch **AwfulError****.****Worse** {
*// Deal with worseness.*
}
catch **AwfulError****.****Terrible** {
*// Deal with terribleness.*
}
catch **ErrorType** {
*// Unexpected error!*
}

H

let** theResult **=** try**!** obj**.****doDangerousStuff**()`

convenience **init**(contentsOfFile path: **String**,
             encoding enc: **UInt**) throws
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,291評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,548評論 4 61
  • 看到這里,我相信你已經(jīng)知道了作用域的概念了,以及根據(jù)聲明的位置和方式將變量分配給作用域的相關(guān)原理了。函數(shù)作用域和塊...
    我就是z閱讀 479評論 1 2
  • 前幾天,司法部發(fā)布了"2016年國家司法考試公告",確定9月24日、25日為今年國家司法考試時間。 看到這一消息,...
    子諾不離閱讀 5,044評論 8 57
  • 名日精進(jìn)【打卡第210天】: 姓名:余成杰 公司:貞觀電器 盛和塾《六項精進(jìn)》224期學(xué)員 【知-學(xué)習(xí)】 《六項精...
    余成杰閱讀 251評論 0 0

友情鏈接更多精彩內(nèi)容