-4

I have a console application with main method as follows:

public static void Main(string[] commandString)
{
    try
    {
        var asm = Assembly.Load("CAT");
        var t = asm.GetType("CAT",true,true);
        var _Adjudication = Activator.CreateInstance(t);
        t.InvokeMember("NAMEME", BindingFlags.InvokeMethod, null, _Adjudication, new object[] { "Fluffy", 5});
    }
    catch (Exception ex)
    {
        Environment.Exit(99);
    }
}

CAT class is as below :

public class CAT
{
    public void NAMEME(string name, int age)
    {
       try
       {
           throw new Exception();
       }
       catch (Exception ex)
       {
           throw ex;
       }
    }
}

I have an error stating "Exception was unhandled by user code" when I run this program. The error is caught by the Catch block of the NAMEME method.

How do I get the exception to hit the catch block of my Main method?

PS : I am using .NET Framework 4.0 PPS: I was able to handle it by capturing the TargetInvocationException error

Max
  • 97
  • 2
  • 13
  • I can't reproduce. It works for me – Jcl Mar 16 '16 at 11:36
  • You should use throw instead of throw ex – Mark PM Mar 16 '16 at 11:36
  • Of course it will catch in the NAMEME method first. It will then throw and be caught in the Main method. Continue to step through your code using F10. – Steve Mar 16 '16 at 12:01
  • i got the same problem for dynamic class, can anyone tell me the problem http://stackoverflow.com/questions/38816233/c-sharp-user-defined-exception-handling-for-erroe-from-dll-get-exception-was-unh?noredirect=1#comment65000685_38816233 – Aylian Craspa Aug 07 '16 at 18:50

1 Answers1

2

This actually works, I copied all the source code and I have it running locally just fine. There are a few typoes in your code provided, but I assume they do not exist in your actual source on your dev box as you said you get the exception to hit in the CAT class. In order for it to hit the next catch in the main app, you need to continue once the exception is thrown -- it will then land where you'd expect it to.

enter image description here

David Pine
  • 23,787
  • 10
  • 79
  • 107
  • Yep, same here, can't repro – Jcl Mar 16 '16 at 11:36
  • In debug mode, I can 'continue' it. But when I run the exe via command line parameters, it wont 'continue' to exit with the error code of 99. – Max Mar 16 '16 at 11:37
  • 1
    Then you must have not given us the full story or something is missing. I literally copied and pasted your code (minus the typo) and then stepped through it as expected. – David Pine Mar 16 '16 at 11:39
  • Okay, I had to get rid of all the actual code in my CAT class as it's work-related.. But the scenario is the same. I invoke a method that throws an exception. File not found / IO .. whatever. The catch block in the CAT class captures it. But Main doesn't get to catch it – Max Mar 16 '16 at 11:42
  • Try the code as you posted it: it works as you'd expect (both to me and to @DavidPine , at least), so there's something in what you have "got rid of" that is making it not work, and we can't guess it. – Jcl Mar 16 '16 at 11:43