import Foundation
enum ErrorScenarios: Error {
case invalidAge
case invalidEmail
case incorrectData
}
func age(age:Int) throws {
if age < 20 {
throw ErrorScenarios.invalidAge
}
print("You have a valid age of \(age)")
}
extension ErrorScenarios: LocalizedError {
var localizedDescription: String {
switch self {
case .incorrectData:
return "Incorrect data provided"
case .invalidAge:
return "Incorrect age was provided"
case .invalidEmail:
return "Incorrect email provided"
}
}
}
Here I'm using a do catch block to catch the invalidAge error scenario, I'm not quite sure if this is correct? When I run the playground the catch block never gets hit.
do {
try age(age: -23)
}
catch {
throw ErrorScenarios.invalidAge
}
Here as an alternative to the do catch block I deal with the error by making it an optional therefore I'm guessing a do catch block is not required. However when printing the result it returns Optional(() rather than an optional Int. Any help would be appreciated where I'm going wrong.
let result = try? age(age: 34)
print(result)