我有一个自定义错误类:
enum RegistrationError :ErrorType{
case PaymentFail
case InformationMissed
case UnKnown
}
我定义了这样一个函数:
func register(studentNationalID: Int) throws -> Int {
// do my business logic then:
if studentNationalID == 100 {
throw RegistrationError.UError(message: "this is cool")
}
if studentNationalID == 10 {
throw RegistrationError.InformationMissed
}
return 0
}
我这样调用这个函数:
do{
let s = try register(100)
print("s = \(s)")
} catch RegistrationError.UError {
print("It is error")
}
我的问题是如何打印抛出异常时抛出的错误消息?
我在快速2
最佳答案
如果捕捉到消息出错,可以按如下方式打印消息:
do{
let s = try register(100)
print("s = \(s)")
} catch RegistrationError.UError (let message){
print("error message = \(message)") // here you will have your actual message
}
但是,即使您没有抛出任何消息,您仍然无法捕获消息,这是如下错误类型:
do{
let s = try register(10)
print("s = \(s)")
} catch RegistrationError.UError (let message){
print("error message = \(message)")
}
catch (let message ){
print("error message = \(message)") //here the message is: InformationMissed
}
关于ios - swift2如何在catch中打印错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33043399/