i'm updating code use swift, , i'm wondering how print error details exception matches 'catch all' clause. i've modified example swift language guide page illustrate point:
do { try vend(itemnamed: "candy bar") // enjoy delicious snack } catch vendingmachineerror.invalidselection { print("invalid selection.") } catch vendingmachineerror.outofstock { print("out of stock.") } catch vendingmachineerror.insufficientfunds(let amountrequired) { print("insufficient funds. please insert additional $\(amountrequired).") } catch { // how print out information error here? } if catch unexpected exception, need able log caused it.
i figured out. noticed line in swift documentation:
if catch clause not specify pattern, clause match , bind error local constant named error
so, tried this:
do { try vend(itemnamed: "candy bar") ... } catch { print("error info: \(error)") } and gave me nice description.
Comments
Post a Comment