Throw vs. Throw Ex

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

This article is part of the GWB Archives. Original Author: Albert Raiani

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.