Skip to content

Instantly share code, notes, and snippets.

@erica
Created June 18, 2015 18:01
Show Gist options
  • Save erica/c5a375ca25456d549e9c to your computer and use it in GitHub Desktop.
Save erica/c5a375ca25456d549e9c to your computer and use it in GitHub Desktop.
import Foundation
// See: https://developer.apple.com/swift/blog/?id=15
struct Error: ErrorType {
var _domain: String {return "com.sadun"}
var _code: Int {return 0}
}
enum MyError : ErrorType {
case Error(reason: String, source: String)
// Thanks Mike Ash!
static func Build(
reason: String, source: String = __FUNCTION__, file: String = __FILE__, line: Int = __LINE__) -> MyError {
return MyError.Error(reason:reason, source:"Thrown in \(source) (File: \(file) Line: \(line))")
}
}
func test() throws -> Int {
let value = arc4random_uniform(2)
if value > 0 {
return numericCast(arc4random_uniform(20))
} else {
throw MyError.Build("Tough luck")
}
}
do { try test() }catch{ print(error) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment