从异常处理程序获取ExitCode [英] Getting ExitCode From Exception Handler

查看:109
本文介绍了从异常处理程序获取ExitCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制台应用程序中有以下错误处理程序

I have following error handler in my console application

主要步骤

AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(ErrorHandler);

处理程序:

static void ErrorHandler(object sender, UnhandledExceptionEventArgs args)
        {
            Exception e = (Exception)args.ExceptionObject;
            ... Loging Error ...
            //Environment.Exit(?);
        }

问题是该错误被记录,但在该应用程序失败后,Windows弹出窗口(应用程序没有响应)显示。

And the problem is that error is logged but after that application failed and windows popup (application is not responding) is showed.

所以我想添加Environment.Exit()来防止这种行为,但是如何从异常中指定exitCode?我不想设置Exit(0)(这意味着一切都可以),因为我也希望在触发此脚本的调度程序应用程序中看到错误(只有一些错误发生的信息,异常已被记录)。

So i want to add Environment.Exit() to prevent that behavior, but how to specify the exitCode from the exception ? I dont want to set Exit(0) (which means everything is okay) because i also want see the error (only information that some error happened, exception is already logged) in the scheduler application which trigger this script.

谢谢

推荐答案

可以使用异常。 HResult

考虑以下代码:

class Program
{
    static int Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; ;

        if (args.Any() && args[0] == "/t")
        {
            Console.WriteLine("I am going to throw an exception");

            throw new ApplicationException("This is an exception");
        }
        else
        {
            Console.WriteLine("I am going to exit");
            //Environment.Exit(0);
            return 0;
        }
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine("An unexpected error has occurred");
        Exception ex = (Exception)e.ExceptionObject;

        Environment.Exit(ex.HResult);
    }

}

然后从批处理文件执行两次 - 一次与/ t参数和另一个没有:

Then execute this from a batch file twice - once with the /t parameter and the other without:

@echo off
cls
echo Running Console Application with no error
ConsoleApplication2.exe
echo %errorlevel%

echo Running Console Application with error
ConsoleApplication2.exe /t
echo %errorlevel%
pause

在第一次运行中,您退出0.我做了主返回一个int并返回0来成功执行 - 你可以在这里执行Environment.Exit(0)。

In the first run you exit with 0. I've made Main return an int and just return 0 for successful execution - you can do Environment.Exit(0) here.

随着第二次运行抛出一个ApplicationException,UnhandledException被处理,然后调用Environment.Exit(ex.HResult)。

With the second run that throws an ApplicationException, the UnhandledException is handled, then Environment.Exit(ex.HResult) is called.

我认为HResult是根据MSDN绑定到特定的异常。但是,根据导致异常的原因,您可能需要不同的退出代码。在这种情况下,您可以抛出自定义异常,然后在UnhandledException处理程序中有一些cafuffled逻辑:

I think the HResult is tied in to specific exceptions as per MSDN. However, you may want different exit codes depending on what caused the exception. In this case you can throw custom exceptions then have some cafuffled logic in the UnhandledException handler:

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine("An unexpected error has occurred");
        Exception ex = (Exception)e.ExceptionObject;
        if (ex is MyException)
        {
            Environment.Exit(10009); // my own exit codes
        }

        Environment.Exit(ex.HResult);
    }

}

class MyException : Exception
{

}

但是,您真的会关心在控制台中失败的原因吗?如果你得到一个未处理的异常,那么它失败了。你可以有很多可能的故障点。你真的要做的错误代码是什么?你已经记录了例外。这提供了重要的信息。

But would you really care why it failed in the console? If you get an unhandled exception then it failed. You can have lots of possible failure points. What are you really going to do with the error code? You have logged the exception out. That provides the vital information.

这篇关于从异常处理程序获取ExitCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆