Throw an exception in Swift
class Car{}
struct Exception: ErrorType {
var message: String
}
class Seller {
var cars: [Car] = [Car]()
func Sell() throws {
if cars.count == 0 {
throw Exception(message: "No cars for sale")
}
}
}
let seller = Seller()
do {
try seller.Sell()
}
catch let e as Exception {
print(e.message)
//e.message is "No cars for sale"
}
catch {}
- Log in to post comments