Created
May 19, 2020 17:43
-
-
Save bestiosdeveloper/33cdbb98b49429c135661b6df39bd0b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum RocketError: Error { | |
case insufficientFuel | |
case insufficientAstronauts(needed: Int) | |
case noAstronauts(message: String) | |
case outOfFuel(message: String) | |
case unknownError(message: String) | |
} | |
func igniteRockets(fuel: Int, astronauts: Int) throws { | |
guard astronauts > 0 else { | |
throw RocketError.noAstronauts(message: "astronauts can not be in -ve!") | |
} | |
guard fuel > 0 else { | |
throw RocketError.outOfFuel(message: "Rocket is out of fuel!!") | |
} | |
if fuel < 1000 { | |
throw RocketError.insufficientFuel | |
} else if astronauts < 3 { | |
throw RocketError.insufficientAstronauts(needed: 3) | |
} else { | |
// Ignite rockets | |
print("3... 2... 1... IGNITION!!! LIFTOFF!!!") | |
} | |
} | |
do { | |
try igniteRockets(fuel: 5000, astronauts: -1) | |
} catch RocketError.insufficientFuel { //OK, earlier as well | |
print("The rocket needs more fuel to take off!") | |
} catch RocketError.insufficientAstronauts(let needed) { //OK, earlier as well | |
print("You need at least \(needed) astronauts...") | |
} catch RocketError.noAstronauts(let message), RocketError.outOfFuel(let message), RocketError.unknownError(let message) { // Also Allowed, Now | |
print(message) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment