Catch the specific exception in Swift
enum Exception: ErrorType {
case isNil, isEmpty
}
class func throwWhenNilOrEmpty(list: [Int]?) throws {
if list == nil {
throw Exception.isNil
}
if list?.count == 0 {
throw Exception.isEmpty
}
}
do {
let list = [Int]()
try throwWhenNilOrEmpty(list)
}
catch Exception.isNil {
print("list is not specified")
}
catch Exception.isEmpty {
print("list is empty")
}
catch {}
- Log in to post comments