Results 1 to 4 of 4

Thread: [RESOLVED] try catch question...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Resolved [RESOLVED] try catch question...

    I have this class:

    Code:
    public class Class1
    {
       public Class(){}
    
       public Func1()
       {
           try
           {
              int x = Convert.ToInt32("a");
           }
           catch(Exception ex)
           {
               throw ex;
           }
        }
    }
    I then call this Func1() using reflection.

    Code:
    try
    {
        //some code that allows me to call method through reflection
       //mi is MethodInfo object and obj is an instance of Class1
      mi.Invoke(obj, null);
    }
    catch(Exception)
    {
       MessageBox.Show(ex.Message);
    }
    When the exception occurs inside Func1(), I throw that exception so it's caught by the next "catch". Why is the exception that is caught outside the Invoke() method not the same exception that I threw? Instead it catches an invoke method exception. Is there a way for it to catch the same exception I threw inside the Func1()?

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: try catch question...

    Would that be an TargetInvocationException and would its InnerException be the FormatException you are looking for?
    W o t . S i g

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: try catch question...

    The MEthodInfo's Invoke method does not let exceptions bubble up through it - that would mean a call to Invoke could throw any exception, which is not the nicest behaviour for the context. Instead, if the method that it represents throws an exception, the Invoke method catches that, wraps it (as Milk says) in a TargetInvocationException (as that was the exception from this method's contract's point of view) and supplies the underlying exception in the InnerException property.

    Think of it like this: The Invoke method says it will invoke the method the MethodInfo instance represents. That's the "contract" between the caller and the Invoke method. If Invoke threw an InvalidCastException (or whatever ToInt32("a") would throw), what does that mean in terms of invoking a method? Instead it throws an exception that says "There was some error from inside the method I was told to invoke, oh and I've included that exception in case you want to see it".



    Also, when re-throwing an exception from inside a catch block, do not specify the exception variable. Just use the throw keyword on its own. By including the exception you cause the stack trace on the exception object to be reset to the current position. This is never what you want. Your code should look like this:

    csharp Code:
    1. public class Class1
    2. {
    3.    public Class(){}
    4.  
    5.    public Func1()
    6.    {
    7.        try
    8.        {
    9.           int x = Convert.ToInt32("a");
    10.        }
    11.        catch(Exception ex)
    12.        {
    13.            // I'm assuming that you also do more processing here
    14.            // and that this is a stripped down example?
    15.            throw;
    16.        }
    17.     }
    18. }

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: try catch question...

    Thanks for the help. "InnerException" was what I was looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width