-
-
Save nilium/8369065a7b7801aa79db to your computer and use it in GitHub Desktop.
Exception handling in Swift.
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 | |
let (succ: String?, err: NSException?) = try { | |
NSException(name: "ExceptionTest", reason: "Exception reason", userInfo: nil).raise() | |
return "Foobar" | |
} | |
if succ { | |
println("Success: \(succ)") | |
} else if err { | |
println("Error [\(err!.name)]: \(err!.reason)") | |
} |
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 | |
func try<T>(block: () -> T) -> (T?, NSException?) { | |
var result: (T?, NSException?) = (nil, nil) | |
tryWithBlock__({ result = (block(), nil) }, { result = (nil, $0) }) | |
return result | |
} |
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/Foundation.h> | |
void tryWithBlock__(void (^block)(void), void (^failure)(NSException *)); |
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 "TryResult.h" | |
void tryWithBlock__(void (^block)(void), void (^failure)(NSException *)) { | |
@try { | |
block(); | |
} | |
@catch (NSException *exception) { | |
failure(exception); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment