18

In Objective-C, Whenever an application crashes, I can get stack trace to see where is the last method that causes the error by using this code in AppDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     NSSetUncaughtExceptionHandler(&myExceptionHandler);
     return YES;
 }

void myExceptionHandler(NSException *exception)
{
    NSArray *stack = [exception callStackSymbols];
    NSLog(@"Stack trace: %@", stack);
    
    NSLog(@"MyExceptionHandler");
}

and it will print the stack trace log to console which I can use to debug the cause of the problem instead of ending up at main.m with no information

So how can I do this in Swift?

4
  • 1
    check : stackoverflow.com/questions/24023112/… Commented Jun 30, 2014 at 9:41
  • Note: Your code only shows stack traces for exceptions, not for other signal based crashes. Commented Jun 30, 2014 at 12:00
  • These objectiveC exception handlers can not be used in swift? Commented May 25, 2020 at 23:17
  • 2
    Answered: stackoverflow.com/questions/25441302/… Commented Aug 6, 2021 at 8:35

1 Answer 1

6

If I understand you correctly, I think what you are looking for is an exception breakpoint, which functions just like a regular breakpoint but is called whenever an exception is thrown. That way, it will stop your application right where the exception was thrown, so you can see the method, line of code, and variable values at the moment of the crash.

This can be set by going to the Breakpoint Navigator tab in the Navigator, clicking the plus at the bottom left and selecting "Add Exception Breakpoint ".

The Exception Breakpoint can than be edited with various options by right-clicking on it and selecting "Edit Breakpoint".

Sign up to request clarification or add additional context in comments.

2 Comments

This is not even close to the same as the StackTrace. The latter is needed in a stringified form in production mode not just inside a playground/sandbox.
It isn't a stack trace but it can kind of fill a similar purpose for some users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.