简体   繁体   English

我应该使用哪个.NET异常来发出外部应用程序故障信号?

[英]Which .NET exception should I use for signaling external application failure?

I'm new to using C# and .NET, but I want to try to play nice and re-use system components. 我是新手使用C#和.NET,但我想尝试玩好并重用系统组件。

In the code I'm maintaining, there are several instances where we run external tools and applications, which looks something like this: 在我正在维护的代码中,有几个实例我们运行外​​部工具和应用程序,看起来像这样:

using (var Setup = Process.Start(SetupInfo) )
{
    Setup.WaitForExit(SetupTimeout);
    if (!Setup.HasExited )
        throw new TimeoutException(
            "Setup did not complete within the expected time");
}

I'm trying to add validation of the exit codes as well for those external tools that have well-defined exit codes, ie something like this inside the 'using' block: 我正在尝试添加退出代码的验证以及那些具有明确定义的退出代码的外部工具,即'using'块中的类似内容:

switch ( Setup.ExitCode )
{
    case 0: 
        break;
    case 1: 
        throw new SetupFailedException("Setup execution failed");
    case 2: 
        throw new SetupFileNotFound("Setup did not find required files");
    default: 
        throw new ExternalErrorException("Setup failed for some other reason");
}

...where the first two would derive from the generic 'ExternalErrorException'. ...前两个派生于泛型'ExternalErrorException'。 But is there some existing generic exception I could re-use for this, ie to signal that an external process has failed to run as expected, rather than invent my own? 但是,是否存在一些我可以重复使用的现有通用异常,即表示外部进程未能按预期运行,而不是发明我自己的?

I don't think there is one you could reuse. 我不认为有一个你可以重复使用。

I think throwing your own exceptions has more value, it allows you to be specific. 我认为抛出自己的异常更有价值,它可以让你具体化。 Your base exception usage is something I like to do too. 您的基本异常使用也是我喜欢做的事情。

Throwing a dedicated exception type only makes sense if there is code somewhere up the call stack that is going to catch the exception and deal with the failure. 抛出一个专用的异常类型只有在调用堆栈的某个地方有代码才能捕获异常并处理失败时才有意义。 Dealing with a gross error like this is quite difficult, you have no idea why the setup program is misbehaving like this. 处理这样的严重错误是非常困难的,你不知道为什么安装程序会像这样行为不端。

Which makes it very likely that there is little point in continuing to execute code, things just go downhill from there. 这使得它可能存在继续执行代码的小点,事情就走下坡路从那里。 After all, an essential program didn't get installed, it is quite liable to throw more exceptions when you try to use it. 毕竟,一个基本的程序没有安装,当你尝试使用它时,很容易抛出更多的异常。 Now without a good explanation why it failed, the true reason was that the installer didn't work. 现在没有一个很好的解释为什么它失败了, 真正的原因是安装程序无法正常工作。

Or, if this is just an auxiliary operation that doesn't otherwise affect your main program, needs to be reported to the user so she can contact IT support to figure out a way around the problem. 或者,如果这只是一个不会影响主程序的辅助操作,则需要向用户报告,以便她可以联系IT支持人员以找出解决问题的方法。

Neither of which requires a dedicated set of exception types. 这两者都不需要一组专用的异常类型。 Only catch exceptions that you know how to recover from. 只捕获您知道如何从中恢复的异常。

Why are you throwing exceptions? 你为什么抛出异常? From what you are saying, it seems you have specific logic that needs to be performed based on the results of the setup. 根据您的说法,您似乎有特定的逻辑需要根据设置的结果执行。 An Exception is not the way to signal business logic. Exception 不是表示业务逻辑的方式。 If anything, I'd have an enumeration that corresponds to the exit codes which can then be checked. 如果有的话,我会有一个与退出代码相对应的枚举,然后可以检查。

If you really have a need to trash the stack and have an Exception bubble all the way up, you can have your method return a value from the enumeration, and then have a wrapper method which would take the value and then map to an Exception which should be thrown. 如果你真的需要删除堆栈并且一直有一个Exception泡泡,你可以让你的方法从枚举中返回一个值,然后有一个包装器方法,它将取值,然后映射到一个Exception ,应该抛出。

Ultimately, if you feel that using a custom Exception is the right way to go, then you should not have a deep Exception hierarchy . 最后,如果您认为使用自定义Exception是正确的方法,那么您不应该有一个深度Exception层次结构 Rather, you would derive from Exception directly. 相反,您将直接从Exception派生。

As someone else has already questioned your motives I shall not do so. 由于其他人已经质疑你的动机,我不会这样做。 I like you often feel compelled to reuse .NET exceptions as that is what feels the most OOP but that is sometimes an excuse because I might be being lazy. 我喜欢你经常感到被迫重用.NET异常,因为这是感觉最大的OOP,但这有时是一个借口,因为我可能是懒惰的。

There's a lot of people that would state that the more verbose your exception is then the better it may be for consuming applications/developers of your code especially if you comment your exception class fully. 有很多人会说你的异常越冗长,那么消费代码的应用程序/开发人员就会越好,特别是如果你完全评论你的异常类。

As Hans mentioned , it doesn't make much sense here to create custom exception types for each case since the calling code is unlikely to act upon the different result cases. 正如Hans所说 ,在这里为每个案例创建自定义异常类型没有多大意义,因为调用代码不太可能对不同的结果案例起作用。 I would go even farther and avoid the use of even a single custom exception type if possible. 如果可能的话,我会更进一步避免使用甚至一个自定义异常类型。

Personally, I would use System.Runtime.InteropServices.ExternalException for all cases, and simply add the failure reason (when available based on the exit code and/or other data) to the exception message. 就个人而言,我会对所有情况使用System.Runtime.InteropServices.ExternalException ,只需将失败原因(如果基于退出代码和/或其他数据可用)添加到异常消息中。 If you ever need to add subclasses for specific cases, these can derive from ExternalException instead of a custom type. 如果您需要为特定情况添加子类,则可以从ExternalException派生而不是自定义类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用哪种方法从.net应用程序远程执行SSIS包? - Which method should I use to execute an SSIS package remotely from a .net application? 在这种情况下应该抛出哪个.Net 2.0异常? - Which .Net 2.0 Exception should I throw in this circumstance? 单元测试时,我应该在ExpectedExceptionAttribute中使用哪种类型的异常? - Which type of exception should I use in ExpectedExceptionAttribute while unit testing ? 在WinForms应用程序中使用外部DLL需要哪种配置? - Which configuration do I need to use an external DLL in a WinForms application? 我应该在 .NET 中使用 ExceptionFilter 还是中间件进行异常处理 - Should I Use ExceptionFilter or Middleware for Exception Handling in .NET 我应该为图表使用哪种Web应用程序技术? - Which web application technology should I use for graphs? 我应该为Windows应用程序使用哪个事件处理程序? - Which Event handler should i use for my windows application? 我应该使用哪种方法将数据库或目录服务(AD LDS(ldap))用于Windows应用程序(.net)的角色管理?为什么? - Which approach should i use database or directory services (AD LDS(ldap)) for role management a windows application (.net)?And Why? 数据库触发器或应用程序代码? 在这种情况下,我应该使用哪一个? - Database trigger or application code? which one should I use in this case? 小型网络应用程序应使用哪个数据库 - Which Database should I use for small network application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM