簡體   English   中英

在csharp中捕獲未處理的異常

[英]catching unhandled exceptions in csharp

我在發布模式下收到NullReferenceException,但在調試模式下卻沒有。 為了嘗試查找錯誤,我添加了UnhandledException處理程序,但是未將異常發送到該處理程序。

沒有gui代碼,它僅是控制台。

程序退出並顯示以下消息:System.NullReferenceException:對象引用未設置為對象的實例。

需要明確的是,我根本沒有在調試模式下得到它,僅當它從控制台以發布模式運行時才得到它,所以我無法對其進行調試以找到問題所在。

為什么設置AppDomain.CurrentDomain.UnhandledException不起作用?

static void errhandler(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    Console.WriteLine(e.StackTrace + ' ' + e.ToString());
    System.Environment.Exit(1);
}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags = System.Security.Permissions.SecurityPermissionFlag.ControlAppDomain)]
static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(errhandler);
        try
        {
            new test(args[0], args[1]);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace + ' ' + e.ToString());
        }

}

為了處理所有異常,我們使用以下代碼:

    private static void SetupExceptionHandling()
    {
        // Add the event handler for handling UI thread exceptions to the event.
        Application.ThreadException += ApplicationThreadException;

        // Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // Add the event handler for handling non-UI thread exceptions to the event.
        AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;

        //  This AppDomain-wide event provides a mechanism to prevent exception escalation policy (which, by default, terminates the process) from triggering.
        //  Each handler is passed a UnobservedTaskExceptionEventArgs instance, which may be used to examine the exception and to mark it as observed.
        TaskScheduler.UnobservedTaskException += TaskSchedulerUnobservedTaskException;

        // Add the event handler for handling UI thread exceptions to the event.
        Dispatcher.CurrentDispatcher.UnhandledException += DispatcherUnhandledException;
    }

希望對您有幫助。

您的代碼是正確的。 證明:

static void errhandler(object sender, UnhandledExceptionEventArgs args)
{
    Console.WriteLine("Errhandler called");

    Exception e = (Exception)args.ExceptionObject;
    Console.WriteLine(e.StackTrace + ' ' + e.ToString());
    System.Environment.Exit(1);
}

public static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(errhandler);

    string s = null;
    s.ToString();
}

打印“ Errhandler被稱為”。

因此,您的問題出在其他地方。

為什么不在代碼中放置try catch塊?

像這樣:

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags = System.Security.Permissions.SecurityPermissionFlag.ControlAppDomain)]
    static void Main(string[] args)
    {
        try
        {
            new test(args[0], args[1]);
        }
        catch(Exception e)
        {
            Console.WriteLine(e)
        }
    }

還是需要使用事件處理程序?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM