我有这个自定义的error类:
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")
}我的问题是如何打印抛出异常时抛出的错误消息?
我在Swift2上
发布于 2015-10-10 00:47:23
如果在消息中发现错误,可以像这样打印消息:
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
}https://stackoverflow.com/questions/33043399
复制相似问题