Swift 2.0异常处理 [英] Swift 2.0 exception handling
问题描述
我们想知道SWIFT中的整个代码块的错误处理机制。
We want to know error handling mechanism in SWIFT for whole block of code.
正如swift中有一些用于错误处理的技术,其中一些就像使用 guard
或如果让
。还可以使用 do-catch
语句来实现。但是我们正在处理 使用单个try-catch块 的错误,正如我们在 Objective-C
。在 Objective-C
中,该块很容易处理任何代码行中的错误。所以我们想知道这种类型的机制。
As, in swift, there are some techniques used for error handling, some of them are like using guard
or if let
. Also we can achieve it using do - catch
statement. But we are getting stuck while handling error for whole bunch of code using single try-catch block as we were doing in Objective-C
. In Objective-C
this block was easily handling errors within any line of code in it. So we want to know this type of mechanism in swift.
现在,我们知道如果我们需要处理错误,那么使用 guard
, if-let
语句,或者写一些自定义的方法,将抛出错误,并在 do-catch
块中使用该方法。那么Swift中是否有任何机制将与 try-catch
块平行,就像在 Objective-C
中一样,所以我们不需要为整条代码中的每一行代码写入 if-let
或 guard
语句
For now, we came to know that if we need to handle error, then use guard
, if-let
statement for each line, or write some custom methods which will throw the error and use that method in do-catch
block. So is there any mechanism in Swift which will be parallel to try-catch
block just like in Objective-C
so we don’t need to write if-let
or guard
statement for each single line of code in that whole bunch of code.
这是我们如何使用 if-let
和 guard
let for single line statement null处理:
This is how we used if-let
and guard
let for single line statement null handling :
if-let
实现示例代码片段: -
if-let
implementation sample code snippet :-
if let title = dict["title"].string
{
let myTitle = title
}
但下面提到我们想要在swift中解决问题的方案 try -catch
阻止代理功能。
But below is mentioned scenario in which we want solution in swift for try-catch
block within delegate functions.
Objective-C
中的示例代码片段,使用 try-catch
块
A sample code snippet in Objective-C
using try-catch
block
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
@try {
static NSString *itemIdentifier = @"CustomCell";
//Some line of code where exception may occur…
//Some line of code where exception may occur…
//Some line of code where exception may occur…
//Some line of code where exception may occur…
//…
//…
//…
return cell;
}
@catch (NSException *exception) {
NSLog(@"Exception in cellForItemAtIndexPath - %@",exception.description);
}
}
以上是我们在 Objective-C $ c中使用的代码片段$ c>但是现在我们想在Swift中使用同样的东西
try-catch
,但到现在为止我们还没有找到任何解决方案,那么我们如何处理这样的情况我们可以添加 try-catch
块来委托表视图,集合视图,...等功能。
Above is the code snippet which we were using in Objective-C
. But now we want to use same thing of try-catch
in Swift. But till now we haven’t found any solution. So how do we handle such scenario in which we can add the try-catch
block to delegate functions of table view, collection view, ... , etc.
推荐答案
正如@JeremyP所说,快速的没有例外,为了使这个清楚,你可以使用一个 do-catch
阻止捕获多个错误,但是必须使用 if-let
和 guard
来避免致命错误不可捕获,例如:
As @JeremyP said, in swift there is no exceptions. To make this clear you can you can use a single do-catch
block to catch multiple errors, nevertheless if-let
and guard
has to be used to avoid fatal errors which are not catchable. For example:
do {
try method1()
try method2()
} catch {
// do something with the error
}
但上一个例子不会处理致命错误,例如:
but the previous example won't handle a fatal error, for example:
do {
try method1()
let myVar = anOptionalVar!
} catch {
}
此代码不会处理一个致命的错误,如果aOptionalVar是 nil
,并强制解开它将导致一个致命的错误,将终止执行。
this code won't handle a fatal error, if anOptionalVar is nil
and you force unwrap it this will cause a fatal error which will terminate execution.
所以@JeremyP描述的模式应该用于处理错误:
So the pattern described by @JeremyP should be used to handle errors:
enum myError: ErrorType {
case BadError
}
do {
guard let myVar = anOptionalVar else { throw MyError.BadError }
} catch MyError.BadError {
// do something with the error
}
所以,从swift 2,没有办法使用 guard
和 if-let
保持安全,免于致命错误,您唯一的选择是使用它们并传播 ErrorType
。
So as of swift 2 there is no way around using guard
and if-let
to stay safe from fatal errors, your only option is using them and propagate an ErrorType
instead.
这篇关于Swift 2.0异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!