An unhandled exception is swallowed (except for the debugger)

I support a legacy C # Winform application using the .NET 4.0 framework. The application works fine, but occasionally due to errors in the application, the application will not be able to work normally when the application creates invalid data in the database.

At certain points, we bind (invalid / corrupted) data to a datagrid, which leads to numerous errors when trying to match a null column to a datagrid column.

An exception of type "System.InvalidCastException" occurred in mscorlib.dll, but was not processed in the user code

Additional information: The object cannot be attributed from DBNull to other types.

Like

An exception of type "System.Reflection.TargetInvocationException" occurred in System.Windows.Forms.dll, but was not processed in the user code

Errors lead to the fact that when running under the Visual Studio debugger, the error is described in detail, and also allows you to see the line in which the error occurred.

, , . , , , . , ( log4net), , , .

stackoverflow, , . , "" .

:

static class Program {
    private static readonly ILog log = LogManager.GetLogger("Main");

    static void Main() {
        // Add some error handlers which log error data to the logfile
        //
        // For UI events (set the unhandled exception mode to force all Windows Forms
        // errors to go through our handler)
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        Application.ThreadException += new ThreadExceptionEventHandler(delegate(object s, ThreadExceptionEventArgs e) {
            ExceptionLogger(e.Exception);
        });
        // Non-UI thread exceptions
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(delegate(object sender, UnhandledExceptionEventArgs e) {
            ExceptionLogger(e.ExceptionObject as Exception);
        });

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        log.Debug("Starting Application");
        Application.Run(mainForm = new MainForm());
    }

    private static void ExceptionLogger(Exception e) {
        log.Error("Unhandled Error", e);
        string errorMsg = "An application error occurred. Please contact the administrator with the following information:\n\n";
        errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
        if (MessageBox.Show(errorMsg, "TLMS Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop) == DialogResult.Abort)
            Application.Exit();
    }

Exception, , , , , / .NET, , , , .

, . ExceptionLogger, , , - . , , , , , , .

x86 (-, x64), 64-, 32- .

+4
1

, " ", . , . , , , .

, InvalidCastException , .NET 4+ -, . (. .NET 4), . -, . MSDN.

EDIT: .NET 3.5 , , .

0

All Articles