Reputation: 1704
I have this custom error class:
enum RegistrationError :ErrorType{
case PaymentFail
case InformationMissed
case UnKnown
}
And I define a function like this:
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
}
I call that function like this:
do{
let s = try register(100)
print("s = \(s)")
} catch RegistrationError.UError {
print("It is error")
}
my question is how to print the error message that I have thrown when I throwed the exception?
I am on Swift2
Upvotes: 0
Views: 100
Reputation: 28799
If you catch an error with a message, you can print the message like this:
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
}
However even if you didn't throw any message, you still cant catch a message, which is the type of the error like this:
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
}
Upvotes: 2