-
-
Save pitiphong-p/15b4cd63bfc90787bbe5 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
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