-
-
Notifications
You must be signed in to change notification settings - Fork 4
try
pannous edited this page Feb 19, 2021
·
1 revision
The try keyword, similar to Ruby, extends the simple Java mechanism and even the advanced Swift mechanism: Instead of
guard let item = inventory[name] else {
throw VendingMachineError.invalidSelection
}
Angle gives you
try item = inventory[name] else invalidSelection
Once an error is assigned to a variable (item), it behaves like an unfulfilled optional:
calling e.g. item.count
without elvis operator .?
activates the throwing of the exception, in this case the invalidSelection Error.
Instead of
guard item.count > 0 else {
throw VendingMachineError.outOfStock
}
In the assert macro, the else block is implicitly throwing:
assert item.count > 0 else outOfStock