且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift中的try-catch异常

更新时间:2023-11-29 14:17:34

它没有异常处理,和开发人员论坛中的这个讨论讨论了为什么会这样


但请记住,Cocoa和Cocoa Touch传统上并不打算
来捕捉异常;他们打算不要让他们首先抛出
。应该使用
可选类型和inout NSError参数来处理普通错误;你应该解决任何
的情况,导致断言失败(这似乎是Swift中唯一的
异常抛出机制)通过编写更好的代码。

but keep in mind that Cocoa and Cocoa Touch traditionally don't intend for you to catch exceptions; they intend for you to not cause them to be thrown in the first place. Ordinary errors should be handled with optional types and inout NSError parameters; you should address any situation that causes an assertion to fail (which seems to be the only exception-throwing mechanism in Swift) by writing better code.