none
Different Exception handling with and without debugger attached RRS feed

  • Question

  • Hi all!
    My application is having a strange behavior and I can't find how to solve it.
    I have a thread that executes an application using AppDomain.CurrentDomain.ExecuteAssembly(app);. I have a try/catch block surrounding this statement and catch for any Exceptions (assuming this are Unhandled exceptions from the application I'm executing.
    Here is the code:

    1try 
    2
    3    AppDomain.CurrentDomain.ExecuteAssembly(AUT.File); 
    4
    5catch (Exception excp) 
    6
    7    logger.Warn("an exception was unhandled: " + excp.ToString()); 
    8
    9 

    The thing is that when I'm executing this code with debugger (in Visual Studio) the behavior is as I expected, i.e, the try/catch block catches unhandled exceptions inside the assembly I'm executing. But If I run it without a debugger the try/catch block doesn't catches unhandled exceptions. But a message box is shown informing of the thrown exception.
    Why this behavior? How can make it have always the same behavior?

    Thanks
    José Tavares
    Tuesday, October 14, 2008 3:31 PM

Answers

  • Juan: thanks for your help anyway.

    nobugz: I've tried that and I've already tried the AppDomain unhandled event handler and none of them worked.
    Is it because the application I'm running is also a Windows Form application with an Application.Run() loop. Maybe the exception is caught in that loop and it doesn't reach my code. Here is an image of the Message box that is thrown:
    MessageBox Exception
    The title of that MessageBox is the title of the application I'm running. Do I have to add
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); 

    to that application main method? I want to test blackbox application. Is there any other way to do this?
    • Marked as answer by jpsstavares Wednesday, October 15, 2008 1:31 PM
    Wednesday, October 15, 2008 9:18 AM
  • The screen-shot you posted is not a message box, it is the dialog that is displayed when the Application class catches an exception.  The ThreadExceptionDialog class.

    Clearly, your problem is located in the "AUT.File" assembly, it's the one that's catching the exception, never giving you a whiff of it.  Maybe it is starting a message loop on another thread with its own Application.Run() method call.  You'll need to ask yourself if you really want to fix that.  Once the exception is allowed to leave that message loop, the UI displayed by that assembly is dead as a doornail and can't be revived.  Anyhoo, you'll need to fix the problem in AUT.File.

    Hans Passant.
    • Marked as answer by jpsstavares Wednesday, October 15, 2008 1:30 PM
    Wednesday, October 15, 2008 12:59 PM
    Moderator

All replies

  • if your logger object use internally Trace or Debug messages, the behavior is not the same depending if you are in debug or release mode.
    If you are en debug mode Trace and Debug messages are enabled, but if you are in release mode only Trace messages are enabled.
    Juan Carlos Ruiz
    Tuesday, October 14, 2008 4:03 PM
  • Well, the logger line of code was just a replacement for my code (too many lines). If I show a MessageBox the behavior is the same: with debugger shows the MessageBox, without debugger shows a system popup.

    I've tried using the UnhandledException Event of AppDomain.CurrentDomain but it isn't triggered.
    Tuesday, October 14, 2008 4:11 PM
  • what is the  string value of: AUT.File ??

    Juan Carlos Ruiz
    Tuesday, October 14, 2008 4:13 PM
  • Is the FullName of the application file I want to execute.
    Note that the application is working just fine both with and without debugger except for the exception handling problem.
    Tuesday, October 14, 2008 4:17 PM
  • what is the full exception message?
    Juan Carlos Ruiz
    Tuesday, October 14, 2008 4:20 PM
  • The goal is to catch any exception because my application is about testing 3rd party applications, so I want to log any exception that is thrown and not handled inside that application.
    In my example the exceptions that are being thrown are: System.ArgumentOutOfRangeException and System.Data.NoNullAllowedException.

    José Tavares
    Tuesday, October 14, 2008 4:30 PM
  • follow all exception messages triying to find etra info about relative paths, permissions, libraries , etc...
    Juan Carlos Ruiz
    Tuesday, October 14, 2008 4:33 PM
  • Sorry only after replied I noticed you asked for the message.
    The message for the ArgumentOutOfRangeException was:

    System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
    Parameter name: SelectedIndex
       at System.Windows.Forms.ListBox.set_SelectedIndex(Int32 value)
       at reml_ref.remlrefmainform.buttonRecipeDown_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    It seems to me it is a normal message just like expected...

    José Tavares
    Tuesday, October 14, 2008 4:44 PM
  • execute te program in both modes and be sure you print this vale:
    AUT.File

    Juan Carlos Ruiz
    Tuesday, October 14, 2008 4:48 PM
  • The value is the same. I even copy the entire application to my application's directory so I don't have access problems.

    José Tavares
    Tuesday, October 14, 2008 4:53 PM
  • may be you have the [AUT.File] assemblly code to review it?

    Juan Carlos Ruiz
    Tuesday, October 14, 2008 4:55 PM
  • Yes I have it. In fact the exceptions that are being thrown were injected by me. I *want* exceptions to be thrown (at least for now), but I want to be able to catch them in *my* code.

    José Tavares

    Tuesday, October 14, 2008 4:58 PM
  • i can't help you any more...
    i need the code (caller and called) to review it.

    Juan Carlos Ruiz
    Tuesday, October 14, 2008 5:12 PM
  • Windows Forms has an exception handler inside the message loop started by Application.Run().  It is aware whether or not a debugger is attached.  Tinker with the Application.SetUnhandledExceptionMode() property.

    Hans Passant.
    • Marked as answer by jpsstavares Wednesday, October 15, 2008 1:31 PM
    • Unmarked as answer by jpsstavares Wednesday, October 15, 2008 1:32 PM
    Tuesday, October 14, 2008 7:44 PM
    Moderator
  • Juan: thanks for your help anyway.

    nobugz: I've tried that and I've already tried the AppDomain unhandled event handler and none of them worked.
    Is it because the application I'm running is also a Windows Form application with an Application.Run() loop. Maybe the exception is caught in that loop and it doesn't reach my code. Here is an image of the Message box that is thrown:
    MessageBox Exception
    The title of that MessageBox is the title of the application I'm running. Do I have to add
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); 

    to that application main method? I want to test blackbox application. Is there any other way to do this?
    • Marked as answer by jpsstavares Wednesday, October 15, 2008 1:31 PM
    Wednesday, October 15, 2008 9:18 AM
  • That's it. If add that line to the application main method all unhandled exceptions reach my code. Is reflection the only way to always reproduce this behavior in every application that I run?

    José Tavares

    Wednesday, October 15, 2008 9:32 AM
  • May be weired solution, still try changing Exception to System.Exception in Catch block

    BRS
    Wednesday, October 15, 2008 11:49 AM
  • Nope.. That didn't work.

    Thanks anyway
    Wednesday, October 15, 2008 12:35 PM
  • The screen-shot you posted is not a message box, it is the dialog that is displayed when the Application class catches an exception.  The ThreadExceptionDialog class.

    Clearly, your problem is located in the "AUT.File" assembly, it's the one that's catching the exception, never giving you a whiff of it.  Maybe it is starting a message loop on another thread with its own Application.Run() method call.  You'll need to ask yourself if you really want to fix that.  Once the exception is allowed to leave that message loop, the UI displayed by that assembly is dead as a doornail and can't be revived.  Anyhoo, you'll need to fix the problem in AUT.File.

    Hans Passant.
    • Marked as answer by jpsstavares Wednesday, October 15, 2008 1:30 PM
    Wednesday, October 15, 2008 12:59 PM
    Moderator
  • Thanks all for your help.
    The scope of my application is to test 3rd party apps so if something like this happens it's a clear sign that I've found a defect in that application. The reason I want to caught any exception is that I want to report those defects.
    I'm OK with the application being shutdown, I'll just re-launch it.
    I think my next approach (although I'll not address it right away) will be to inject the SetUnhandledExceptionMode line of code in the main method of the application I'm testing before executing it. I'll just have to figure what I'll do if the application already changes it.

    Again, thanks for your help,
    José Tavares
    Wednesday, October 15, 2008 1:30 PM