我应该使用哪个 .NET 异常来表示外部应用程序失败?

Chr*_*fer 7 c# exception .net-4.0

我刚开始使用 C# 和 .NET,但我想尝试好好发挥并重用系统组件。

在我维护的代码中,有几个我们运行外​​部工具和应用程序的实例,如下所示:

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

我正在尝试为那些具有明确定义的退出代码的外部工具添加退出代码的验证,即“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");
}
Run Code Online (Sandbox Code Playgroud)

...其中前两个将派生自通用的“ExternalErrorException”。但是,是否有一些现有的通用异常我可以重复使用,即表明外部进程未能按预期运行,而不是发明我自己的异常?

小智 2

我认为没有一个可以重复使用。

我认为抛出你自己的异常更有价值,它可以让你变得具体。你的基本异常用法也是我喜欢做的事情。