@try@catch block not catching inside exception

Tony Friz

OK, I've never seen this before. Here is my code:

    @try {
        [self.avPlayer removeObserver:self forKeyPath:@"status"];
        [self.avPlayer removeObserver:self forKeyPath:@"rate"];
    } @catch (NSException *exception) {

    } @finally {

    }

You can quite clearly see, I have those 2 lines of code in the @try block. However, it's still crashing my app. Here's the uncaught exception message:

Cannot remove an observer <SoulHLECellView 0x7fa8d2b536b0> for the key path "status" from <AVPlayer 0x7fa8d714ad70> because it is not registered as an observer.

So, very clearly, this is being caused by the code I've posted above. How the heck is this possible? What am I missing here?

fullofsquirrels

To answer your question as to why you aren't actually catching the exception, there are certain types of exceptions that cannot be caught; see http://www.cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html for a pretty good explanation of what these are, what their causes are, and what to do about them. I suspect that the exception that's actually getting thrown is a runtime exception that you can't actually handle with @try/@catch.

To triage, you can try setting a breakpoint on -[NSException raise] in CoreFoundation or objc_exception_throw in libobjc.A.dylib, and more than likely that breakpoint will capture what the actual exception is and give you a clue as to why you aren't catching it.

All that being said, @gnasher729 hit the nail on the head as to how best to handle this type of situation in the long run (i.e. code that you want to actually release out into the world).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Catching an Exception with try-catch inside an if/switch block

Exception not catching in try/catch block

Nested Try-Catch Block Not Catching Exception

Catching multiple exception types in one catch block

How to throw an Exception inside a Try/Catch block?

Try catch not catching exception with input

Catching exception inside a decorator

Java Exception inside catch block

How do I move back to try block after catching exception

PHP PDO try catch block not catching

Scala try-catch not catching exception

nested try catch catching the same exception

Multiple awaits inside try catch not catching error

Not catching exceptions even though it is inside try catch?

try except not catching the exception

Exception thrown inside catch block - will it be caught again?

How is it possible that the Exception is null inside of a catch block?

how do I add a try catch block inside a for loop to catch an ArrayIndexOutOfBounds exception?

Groovy catch block not catching MultipleCompilationErrorsException

Unhandled exception in try/catch block

Retry on exception in a try catch block

Exception not caught in try catch block

Try block is not catching a filesystem error

Except block not catching exception in ipython notebook

Catching inner exception in F# async block

Check for exception type inside Exception catch block and log it

Why catch block of base class is catching the exception when I am throwing object of derived class?

can we throw the exception object to calling method even after catching it in the catch block?

Catching Error inside Else and Try