swift抛出异常,Swift中的try-catch异常

Is it possible to catch exceptions in Swift? Given the following code:

NSException.raise(NSRangeException,

format: "Now you've gone too far!",

arguments: CVaListPointer(fromUnsafePointer: UnsafePointer()))

Is it possible to prevent the exception from crashing the entire program? That is, what is the Swift equivalent of the following in Objective-C:

@try {

[NSException raise:NSRangeException format:@"Now you've gone too far!"];

}

解决方案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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值