简体   繁体   English

调试只发生在exe但不是来自IDE的异常

[英]Debugging an exception that only happens to exe but not from IDE

on closing the main form of the application(which I call so many methods on it,etc) if I run my application from IDE and want to close it, it is ok but if I just run the exe file It will throw an exception. 关闭应用程序的主要形式(我在其上调用了很多方法等)如果我从IDE运行我的应用程序并想要关闭它,它没关系,但如果我只是运行exe文件它将抛出异常。

so what way do you propose to me for debugging it? 那么你建议我以什么方式进行调试呢? as I said when I run it from the IDE, it is Ok and no error occurs 就像我说的,当我从IDE运行它时,它是好的,没有错误发生

Two things I can think of to try: 我能想到的两件事要尝试:

  • Run the application from outside the IDE but then attach to the process. 从IDE外部运行应用程序,然后附加到该进程。 It could be that when starting from the debugger the environment will be different in some way 可能是从调试器启动时环境会以某种方式不同
  • Use adplus (see my earlier post here to catch the crash dump so you can analyse it later 使用ADPlus(见我以前的帖子这里赶崩溃转储以便以后可以分析它

Find out what the exception is, to start with. 找出异常是什么,开始。 Can you already see the exception details? 你能看到异常细节吗? Does it offer you the option of attaching to the debugger? 它是否为您提供附加到调试器的选项? Can you catch the exception and log it? 你能抓住异常并记录下来吗?

Attach the debugger after you got the program started. 程序启动后附加调试器。 This ensures any side-effects, like the startup directory, the hosting process and JIT optimization cannot be affected by the debugger. 这可以确保调试器不会影响任何副作用,如启动目录,托管进程和JIT优化。

Start your program. 开始你的程序。 Tools + Attach to Process. 工具+附加到流程。

I have a solution written in C++-CLI which should be easy enough to port to C#. 我有一个用C ++编写的解决方案 - CLI应该很容易移植到C#。

If it's happening within the main function itself, have you tried wrapping all your code in a: 如果它发生在main函数本身中,您是否尝试将所有代码包装在:

try
{
    main();
}
catch( System.Exception^ e)
{
    // do something
}

Apologies for the C++-ish-ness of my answer - it's been a long time since I wrote any C# ;-) 为C ++道歉 - 我的答案是什么 - 自从我写了任何C#以来已经很久了;-)

You should be able to attach a global exception handler: 您应该能够附加全局异常处理程序:

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

And then create a function to handle the exception: 然后创建一个函数来处理异常:

private void Application_ThreadException(object sender,System.Thread.ThreadExceptionEventArgs e) {
    // Do whatever here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM