2

Is it possible to get which class thrown the exception at runtime?

If so, then how to do it?

What I want to do is to detect which class did throw the exception. I'm using NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler *) to detect if my app crashes. With that, I have the NSException and I want to know which class throws it.

Also, it is not for debugging purposes.

#pragma mark - Exception Handler
void HandleException(NSException *exception)
{
    // Handle exception
}

void InstallUncaughtExceptionHandler()
{
    NSSetUncaughtExceptionHandler(&HandleException);

    //signal(SIGSYS, SIG_DFL);

    for(int x = 1; x <= 30; x++)
    {
        signal(x, SIG_DFL);    
    }
}
dzep
  • 685
  • 1
  • 8
  • 20
  • 2
    Are you looking for a Boolean answer? If so, then the answer is "yes". – user3447428 Mar 24 '14 at 10:44
  • Look here: http://stackoverflow.com/questions/14269647/debugging-in-xcode-exception-breakpoints – Marco Pace Mar 24 '14 at 10:45
  • @user3447428, Thanks! I updated my question. Basically, I need to know what to do it. – dzep Mar 24 '14 at 10:46
  • @MarcoPace, thanks but what I want to do is to detect which class did throw the exception. I'm using NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler *) to detect if my app crashes. With that, I have the NSException and I want to know which class throws it. – dzep Mar 24 '14 at 10:52
  • @dzep look at my code and adopt it to a set exception handler. – Amin Negm-Awad Mar 24 '14 at 10:55
  • @dzep: once the exception is raised, you can read your stack to know which class / method / line raised the exception – Marco Pace Mar 24 '14 at 11:25
  • What do you actually want to do with this information? You don't need it for debugging. And if your app crashes because of an uncaught exception (or basically because of an exception, because Cocoa says that exceptions are used for bugs in the code and shouldn't be caught), your app should behave like any other app in that situation. – gnasher729 Mar 24 '14 at 12:39

3 Answers3

1

You can read the stack trace with +callStackSymbols (NSException) inside an exception handler. The last entries are system calls. But beneath it you will find the thrower.

@try
{
    …
}
@catch (NSException *e)
{
    NSLog( @"%@", [e callStackSymbols] );
}
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • Thanks! Yup, we can read it using `callStackSymbols`. And we know that submitted app (.ipa) doesn't symbolicate it at runtime. So, if we got the call stacks, we still don't know which class thrown the exception. If only iTunes crash log has a REASON info. – dzep Mar 24 '14 at 10:59
  • Oh, I thought that it is for debugging purpose. – Amin Negm-Awad Mar 24 '14 at 11:03
  • But you can see the return address? (I cannot test it right now.) So you can iterate through the classes and its implementation pointers of the methods and find a match. (You have to do a bounds check, yes.) Sounds ugly on one hand, having an exception you have enough time to do that on the other hand. Would this work for you? I never tried it, but thought about it facing a problem in past. – Amin Negm-Awad Mar 24 '14 at 11:12
0

You don't have much control over where that breakpoint triggers. Once you've hit the breakpoint though, you can use the bt command to print the current stack trace.

Very good answer from this link.

Community
  • 1
  • 1
Dhanashree
  • 79
  • 1
  • 5
0

Just go to breakpoint tap pane on the left side panel of Xcode. Press "+" putton in the bottom-right corner of that panel. Click on "add exception breakpoint". Every exception your app will raise, a breakpoint will automatically stop the execution and you can see where that exception is in your code!

Jacopo Berta
  • 265
  • 2
  • 14