堆栈跟踪或有关 Xcode/iPhone 中未处理异常的更多信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1093999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stack trace or more info on unhandled exception in Xcode/iPhone
提问by Philippe Leybaert
Excuse my ignorance, but something's been bugging me about the Xcode debugger when running iPhone applications in the iPhone Simulator.
请原谅我的无知,但在 iPhone 模拟器中运行 iPhone 应用程序时,Xcode 调试器一直困扰着我。
Sometimes, when I mess something up in Interface Builder, I get an unhandled exception at runtime and I get thrown back to Xcode. The only thing I see is a single line "uncaught exception" or something like that. From what I can see, there's no additional information, let alone a stack trace or anything else that's useful.
有时,当我在 Interface Builder 中搞砸了一些东西时,我会在运行时收到一个未处理的异常,然后我被扔回 Xcode。我唯一看到的是一行“未捕获的异常”或类似的东西。据我所知,没有其他信息,更不用说堆栈跟踪或其他任何有用的信息。
I have been developing in Visual Studio for the last decade or so, and I'm used to getting a nice stack trace and complete exception info when something like that happens.
在过去的十年左右的时间里,我一直在 Visual Studio 中进行开发,并且我习惯于在发生类似事情时获得很好的堆栈跟踪和完整的异常信息。
I'm sure I'm simply missing something very obvious... Hopefully...
我确定我只是遗漏了一些非常明显的东西......希望......
回答by Brad Larson
If you add two breakpoints, you should be able to debug these exceptions. To do this, go to Run | Show | Breakpoints and create two global breakpoints (I do them globally because they are so useful in all my applications). The first should be named "objc_exception_throw" and its location should be "libobjc.A.dylib". The second should be "-[NSException raise]" and its location should be "CoreFoundation".
如果添加两个断点,您应该能够调试这些异常。为此,请转到运行 | 显示 | 断点并创建两个全局断点(我全局执行它们,因为它们在我的所有应用程序中都非常有用)。第一个应命名为“objc_exception_throw”,其位置应为“libobjc.A.dylib”。第二个应该是“-[NSException raise]”,它的位置应该是“CoreFoundation”。
Now, if you start debugging your application with breakpoints enabled, it should break on the throw of these exceptions. You should then be able to see the chain of events that led to the exception within the debugger.
现在,如果您在启用断点的情况下开始调试您的应用程序,它应该会在抛出这些异常时中断。然后,您应该能够在调试器中看到导致异常的事件链。
回答by memmons
The lack of a stack trace is usuallyindicative of a problem with LLDB(debugger). I love LLDB, but when it comes to showing stack traces and breaking on the exception rather than main in iOS apps, it's a pain in the ass and has been for a few releases now. No idea why Apple hasn't addressed this yet. To fix it is a two-step process:
缺少堆栈跟踪通常表明LLDB(调试器)存在问题。我喜欢 LLDB,但是当涉及到在 iOS 应用程序中显示堆栈跟踪和打破异常而不是主要时,这很麻烦,现在已经有几个版本了。不知道为什么苹果还没有解决这个问题。修复它是一个两步过程:
- Edit your current scheme and under the "Run" tab change the debugger from LLDB to GDB.
- Go to https://developer.apple.com/bugreporter/and report the bug so Apple addresses it.
- 编辑您当前的方案并在“运行”选项卡下将调试器从 LLDB 更改为 GDB。
- 转到https://developer.apple.com/bugreporter/并报告错误,以便 Apple 解决它。
回答by Ben Gotow
Hey activa - for more information about runtime exceptions, you should be able to open the debugger console and see more information. I assume you've already done that, but just in case - you can get to it by selecting Run -> Console from the menu. I'm not sure why it doesn't come up automatically!
嘿 activa - 有关运行时异常的更多信息,您应该能够打开调试器控制台并查看更多信息。我假设您已经这样做了,但以防万一 - 您可以通过从菜单中选择 Run -> Console 来访问它。我不知道为什么它不会自动出现!
回答by Remus Rusanu
You can wrap your UIApplicationMain in a try/catch:
您可以将 UIApplicationMain 包装在 try/catch 中:
int main(int argc, char *argv[]) {
int retVal;
NSAutoreleasePool * pool;
@try
{
pool = [[NSAutoreleasePool alloc] init];
retVal = UIApplicationMain(argc, argv, nil, nil);
}
@catch(NSException* e)
{
NSLog(@"%@", e.reason);
}
@finally
{
[pool release];
}
return retVal;
}
You should also look up into setting an assertion handler while debugging: NSAssertionHandler.
您还应该在调试时查看设置断言处理程序:NSAssertionHandler。
Update: and also the unhandled exception handler: NSSetUncaughtExceptionHandler
更新:还有未处理的异常处理程序:NSSetUncaughtExceptionHandler