Catching general exception in swift

I understand that general case for catching general exception is:

do {

try yourmethod

} catch {

// handle error

}

But is there a way to get exception inside catch block. Also, this exception may be coming from swift code or underlying library so I would like a way to handle both. (I can add catch to handle swift errors but in this case I don't know the exception the method might throw).



Thanks.

a. It's an error, not an exception.


b. As documented, inside your default 'catch' you can refer to the thrown error as 'error':


… catch {
     debugPrint ("Caught: \(error)")
}

To catch swift errors:


do {
    try foo()
} catch let error {
    print(error)
}


You cannot, as far as I know, catch ObjC exceptions in Swift. If you have an ObjC exception, that's supposed to be handled like a programming error, and assert in the general case.

Thanks, this worked for me.

Important: Don't think of this construct as exceptions. It is only sugar around NSError-arguments.

Catching general exception in swift
 
 
Q