转发Swift中的错误 [英] Forwarding an error in Swift
本文介绍了转发Swift中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这一刻,我正在这样做:
枚举错误:ErrorType {
case Error1
case Error2
}
func func1()throws {
do {
try func2()
} catch Error.Error1 {
throw Error.Error1
} catch Error.Error2 {
throw Error.Error2
}
}
func func2()throws {
// proof something
throw Error.Error1
}
所以,要转发错误,我需要抓住所有错误并再次抛出。
有更好的解决方案吗?
解决方案
是的,不要将它包装在一个... catch块中。
func func2 )throws {
// proof something
throw Error.Error1
}
func func1()throws {
t ry func2()
}
Is there a better solution to forward a Swift error from one function to another?
In the moment, I'm doing it like this:
enum Error:ErrorType{
case Error1
case Error2
}
func func1()throws{
do{
try func2()
}catch Error.Error1{
throw Error.Error1
}catch Error.Error2{
throw Error.Error2
}
}
func func2()throws{
// proof something
throw Error.Error1
}
So, to forward an error, I need to catch all the errors and throw them again.
Is there a better solution?
解决方案
Yes: don't wrap it in a do ... catch block.
func func2() throws{
// proof something
throw Error.Error1
}
func func1()throws{
try func2()
}
这篇关于转发Swift中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文