推 sohumi:use try catch 06/14 16:05
推 tom19830924:實作上我從來沒用過try-catch... 06/14 16:49
→ charlesdc:你的case不需要用到 實做上會用到的機率很小 06/14 17:30
→ charlesdc:會用到的情境大概是某段會crash但有某種理由要繼續下去 06/14 17:32
→ nobody1:try catch 掛的時候還是一樣會掛掉的 06/14 17:55
→ yuanruo:我只有在連api時會用try-cache 而且只有用過一次... 06/14 17:55
→ yuanruo:ios好像不會try cache中拋出exceptions 所以幾乎沒用 06/14 17:58
→ charlesdc:會 只有APP要crash的時候 06/14 23:37
→ charlesdc:應該是我碰到的case太少@@ 06/15 01:06
http://goo.gl/ZV1yL [developer.apple.com]
If you’re coming from other platforms and languages, you may be used to
working with exceptions for the majority of error handling. When you’re
writing code with Objective-C, exceptions are used solely for programmer
errors, like out-of-bounds array access or invalid method arguments. These
are the problems that you should find and fix during testing before you ship
your app.
Objective-C 的 exceptions 在本質上和 C++ 或 Java 或其他語言不一樣
寫 Objective-C 程式時的慣例是用 NSError 來表達失敗狀態
跳 exception 基本上就代表程式要炸了, 是最後一道防線
讀寫檔失敗這種可以補救的請不要隨便使用
像 NSData 的讀寫檔 methods 都有一個包含 error 回傳值的形式可以使用
還有另外一個考量點是 Objective-C 的 try-catch 相對很耗資源
(和 introspection 與 error-checking 比起來)
--
"問おう、貴方が私のマスターか?"
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.81.146
※ 編輯: uranusjr 來自: 114.32.81.146 (06/15 01:54)