Geeks With Blogs
Albert Raiani blog

Being the good programmers that you are you like to write your code so that it is as readable as possible, so you do things like qualifying your private instance variables with Me (this in C#)  even though it’s optional.  Sometimes you may even use the syntax “For i = x to y…next i” just to show which loop you are currently iterating.  So it’s only natural, when throwing exceptions to want to specify the exception instance that you are throwing, for example Throw ex.  What you may not realize is that there is a difference between “Throw ex” and “Throw”.

The runtime generates a stack trace and maintains it from the point at which the exception is thrown.  Whenever you re-throw an exception the call stack is cleared to the point at which the exception was handled and re-thrown.  Therefore to keep the stack trace intact when re-throwing an exception the correct syntax is to use the “Throw” keyword without specifying an exception.

Following is some sample code to demonstrate the point.  Notice that in all methods except for Sub Main() the exception is re-thrown using simply “Throw”.  When you examine the output Sub1(), Sub2() and Sub3() all maintain the entire stack trace.  But when Sub Main() handles and re-throws the stack trace only includes the call to Sub1 and itself.

 

Module Module1

 

    Sub Main()

        Try

            Console.WriteLine("Beginning Main()")

            Sub1()

        Catch ex As Exception

            Console.WriteLine()

            Console.WriteLine("Handling exception in Sub Main()")

            Console.WriteLine(ex.Message)

            Console.WriteLine(ex.StackTrace)

        End Try

 

        Console.ReadLine()

    End Sub

 

    Sub Sub1()

        Try

            Sub2()

        Catch ex As Exception

            Console.WriteLine()

            Console.WriteLine("Handling Exception in Sub1()")

            Console.WriteLine(ex.Message)

            Console.WriteLine(ex.StackTrace)

            Throw ex

        End Try

    End Sub

 

    Sub Sub2()

        Try

            Sub3()

        Catch ex As Exception

            Console.WriteLine()

            Console.WriteLine("Handling Exception in Sub2()")

            Console.WriteLine(ex.Message)

            Console.WriteLine(ex.StackTrace)

            Throw

        End Try

    End Sub

 

    Sub Sub3()

        Console.WriteLine()

 

        Dim i() As Integer = {1, 2, 3}

        Try

            For x As Integer = 0 To 3

                Console.WriteLine(i(x))

            Next x

        Catch Ex As Exception

            Console.WriteLine()

            Console.WriteLine("Handling Exception in Sub3()")

            Console.WriteLine(Ex.Message)

            Console.WriteLine(Ex.StackTrace)

            Throw

        End Try

    End Sub

 

End Module

 

 

Posted on Thursday, May 10, 2007 8:10 AM | Back to top


Comments on this post: Throw vs. Throw Ex

# re: Throw vs. Throw Ex
Requesting Gravatar...
Using "Throw ex" certainly does lose a lot of info, but if you change them all to use "Throw New Exception("Handling Exception in...",Ex) you'll get more info. The best thing you'll get is the line number that actually caused the exception to occur. There is a performance hit I suppose, but hopefully this code won't be running very orften. :-)
Left by Andrew on Feb 26, 2009 1:32 PM

Your comment:
 (will show your gravatar)


Copyright © Albert Raiani | Powered by: GeeksWithBlogs.net | Join free