williamFalcon / SwiftTryCatch

Adds try-catch support for Swift
MIT License
140 stars 58 forks source link

It won't do whats its supposed to #6

Closed DanielEdrisian closed 6 years ago

DanielEdrisian commented 9 years ago

In the try code i still get an exception and the app crashes instead of doing the code in catch. PleAse hElp mE

zdesiree commented 9 years ago

Same here :( This simple Array index out of range crashes

SwiftTryCatch.try({
            var array = ["test"]
            var wrongThing = array[1]

            }, catch: { (error) -> Void in
                println("\(error.description)")
            }, finally: {
                println("finally")
        })
ArkadiYoskovitz commented 8 years ago

you need to change the method signature, it shouldn't include language reserved words.

This worked for me: in the header: +(void)tryRun:(void (^)())tryRun catchRun:(void (^)(NSException *))catchRun finallyRun:(void (^)())finallyRun;

in the implementation: +(void)tryRun:(void (^)())tryRun catchRun:(void (^)(NSException ))catchRun finallyRun:(void (^)())finallyRun { @try { tryRun ? tryRun() : nil; } @catch (NSException exception) { catchRun ? catchRun(exception) : nil; } @finally { finallyRun ? finallyRun() : nil; } }

in swift code:

SwiftTryCatch.tryRun({ // code to try }, catchRun: { (error) in // code to handle exception }, finallyRun: { // code to do any way })