Tutorial :Debugging an exception that only happens to exe but not from IDE



Question:

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.

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


Solution:1

Two things I can think of to try:

  • Run the application from outside the IDE but then attach to the process. 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


Solution:2

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?


Solution:3

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.

Start your program. Tools + Attach to Process.


Solution:4

I have a solution written in C++-CLI which should be easy enough to port to C#.

If it's happening within the main function itself, have you tried wrapping all your code in a:

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# ;-)


Solution:5

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  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »