none
Problem with rethrowing inner exception RRS feed

  • Question

  • I have some trouble rethrowing inner exception which is comming from a reflection method call.

     

    Protected Sub InvokeDocumentBeginPrint(ByVal e As Printing.PrintEventArgs)
        Try
            Dim mi As System.Reflection.MethodInfo = GetType(Printing.PrintDocument).GetMethod("OnBeginPrint", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
            mi.Invoke(MyPrintDocument, New Object() {e})
        Catch ex As Exception
            Throw ex.InnerException

        End Try

    End Sub

     

     

    If something goes wrong inside of the reflection call, it always throw an exception type TargetInvocationException with useful information in InnerException Property. So I only want the InnerException part, however, if I do this, it will stop execution on End Try line in Debug mode and dislay JIT message in Release on the same End Try line.

     

    I do not want the users to think it crashes even though it works fine by press continue on JIT debugger.

    Am I taking a wrong approach?

    Monday, May 5, 2008 4:29 PM

All replies

  • You'd need a Try/Catch statement in the function that called InvokeDocumentBeginPrint() to prevent this from happening.
    Monday, May 5, 2008 11:43 PM
    Moderator
  • Would you be more specific where I should put Try/Catch?

    What I really want to do is re throw, not catch it here and handle it.

     

    Tuesday, May 6, 2008 3:04 AM
  • Why do you throw ex.InnerException? I believe this exception is not hanlded in method caller. As nobugz mentioned,

    you need to place InvokeDocumentBeginPrint in try/catch block and handle this exception.

    Tuesday, May 6, 2008 8:08 AM
  • In this case the top level exception does not contain any valueable information since it will always be TargetInvocationException, which I do not really care.

     

    I have a try/catch block on the top level where I start the application. Even though the try/catch block only displays a message say that it encounters error, every exception is handled.

     

    The code structure is like below (Not exactly the same but it will give you some idea)

     

    Private Sub DoPrint()

    Try

    Dim pea As New Printing.PrintEvetArgs()

    InvokeDocumentBeginPrint(pea)

    Dim ppea As Printing.PrintPageEventArgs

    InvokeDocumentPrintPage(ppea)

    InvokeDocumentEndPrint(pea)

    Catch ex As Exception

    MessageBox("Error")

    End Try

    End Sub

     

    Protected Sub InvokeDocumentBeginPrint(ByVal e As Printing.PrintEventArgs)

    Try

    Dim mi As System.Reflection.MethodInfo = GetType(Printing.PrintDocument).GetMethod("OnBeginPrint",

    Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)

    mi.Invoke(MyPrintDocument, New Object() {e})

    Catch ex As Exception

              Throw ex.InnerException

    End Try

    End Sub

     

    The red line is where the execution stops, which means that the code does not even get back to the try catch block of caller. It is actually Okay in the development environment as you can just press F5 to make it crash and the caller block to handle situation. However, in release, JIT dialog shows up and it does not look nice, and users are scrambled with a big red X. Also, even after they press Continue on JIT dialog, it still crashes, and it does not work the same as in debug mode. I have a feeling that the solution for this problem is not something simple as put a try/catch block somewhere.

     

    Any idea would be appriciated.

    Tuesday, May 6, 2008 1:44 PM