1

Background

I am developing software running in a larger executive program, to which I can communicate using only its API. After extensive testing, I've determined there is an architecture problem in the executive concerning how it handles exceptions.

The executive program has its own exception handling in the form of a pop-up dialog providing the user with information about the exception. Based on the program's behavior, I suspect it resembles this:

// Executive Program
try {
    // A simple state machine controlling execution mode within the Executive Program
    // Not actually certain a switch is used, but this is representative of behavior
    switch(executiveStateMachine){
        case setUp:
            // [....]
            break;
        case starting:
            // [....]
            break;
        case run:
            while(true){
                // [...]
                // My software running inside executive program
                // [...]
                // Eventual break after successful completion
                break;
            }
            break;
        case finish:
            // [....]
            break;
    }
}
    catch(Exception except){
        // On any, show a proprietary exception dialog
        if(ExceptionDialog.ShowDialog(Exception) == DialogResult.OK) break;
    }

When handling exceptions, it appears the Executive Program occasionally creates fatal errors within its state machine transitions causing the entire program to crash.

While I've filed a bug request with the Executive's Developer, I don't think it will be fixed any time soon and would really like to handle both my own software's exceptions and the Executive Program's exceptions myself.

Question; (TLDR)

Is it possible in C# to catch an exception using some method before a larger, encapsulating (and unseen) try-catch block can?

wallyk
  • 56,922
  • 16
  • 83
  • 148
JGSandov
  • 27
  • 2
  • Yes use a try-catch in your code. – L.B Aug 16 '17 at 22:03
  • Doesn't answer your question, but the MethodInfo approach discussed in the accepted answer here https://stackoverflow.com/questions/4117228/reflection-methodinfo-invoke-catch-exceptions-from-inside-the-method might be helpful. It sandboxes exceptions, which MAY solve your problem (but may not) – zzxyz Aug 16 '17 at 22:05
  • @L.B: Using a try-catch block in my code would work for exceptions in my software, but I'd really like to catch exceptions from the outer Executive program as well. – JGSandov Aug 16 '17 at 22:05
  • @JGSandov as a last resort `AppDomain.CurrentDomain.UnhandledException` – L.B Aug 16 '17 at 22:07
  • There's no built-in way to do this. If the executive is actually catching and handling the exceptions, and they don't occur while your code is in the call stack, there's no event later that would report the exception to you. I have no doubt that it would be possible to do some code-injection, in which you replace the executive handler for their try/catch with your own, but without their cooperation, that's going to be an uphill battle and dependent on their implementation details. Certainly, there's no practical way for Stack Overflow to provide advice on such a broad goal as that. – Peter Duniho Aug 17 '17 at 00:32

0 Answers0