Created
June 26, 2015 03:39
-
-
Save rgm/b17f0a726f37fc99cac7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env xcrun swift | |
protocol SomeType { } | |
extension SomeType { | |
func maybeGreeting(noReally: Bool) -> String? { | |
return (noReally ? "howzit" : nil) | |
} | |
} | |
protocol BaseError { } | |
extension BaseError { | |
// minimum requirements to conform to ErrorType | |
var _domain : String { return "theDomain" } | |
var _code : Int { return 12 } | |
} | |
struct MyError : ErrorType, BaseError { } | |
struct MyOtherError : ErrorType, BaseError { } | |
struct MyThing : SomeType { | |
func greet(shouldThrow: Bool) throws { | |
defer { print("deferred 1") } | |
if let unwrappedGreeting = maybeGreeting(shouldThrow) { | |
print(unwrappedGreeting) | |
throw MyOtherError() | |
} else { | |
defer { print("deferred 2") } | |
throw MyError() | |
} | |
} | |
} | |
do { | |
let shouldThrow = true | |
try MyThing().greet(shouldThrow) | |
} catch is MyError { | |
print("caught myerror") | |
} catch is MyOtherError { | |
print("caught myothererror") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment