What .NET exception should be used to signal an external application failure?

I am new to using C # and .NET, but want to try to reproduce nice and reuse system components.

In the code that I support, there are several cases where we run external tools and applications that look 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 am trying to add an exit code check also for those external tools that have well-defined exit codes, i.e. something like this inside the using block:

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");
}

... "ExternalErrorException". - , , , , , , ?

+3
5

, .

, , . - , .

+3

, - , . , , , , .

- , , . , , , . , , , .

, , , , -, .

. , , .

+2

? , , , , . Exception -. , , , .

Exception , , -, , Exception, .

, , Exception - , Exception. , Exception .

+1

- , . , .NET-, , , , .

, , , / , .

0

, , , , .

Personally, I would use System.Runtime.InteropServices.ExternalExceptionfor all cases and simply add the reason for the failure (when it was available based on the exit code and / or other data) in the exception message. If you need to add subclasses for specific cases, they can be obtained from ExternalExceptioninstead of a custom type.

0
source

All Articles