Skip to content

Instantly share code, notes, and snippets.

@pitiphong-p
Forked from erica/error.swift
Created November 14, 2015 08:57
Show Gist options
  • Save pitiphong-p/15b4cd63bfc90787bbe5 to your computer and use it in GitHub Desktop.
Save pitiphong-p/15b4cd63bfc90787bbe5 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