Skip to content

Instantly share code, notes, and snippets.

@rgm
Created June 26, 2015 03:39
Show Gist options
  • Save rgm/b17f0a726f37fc99cac7 to your computer and use it in GitHub Desktop.
Save rgm/b17f0a726f37fc99cac7 to your computer and use it in GitHub Desktop.
#!/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