Throwing Yourself

I had a stupid idea a day ago: could I write code where "Throw Me" would be valid? Turns out it's not that hard:

Imports System.Runtime.Serialization

Public Class ACustomException
    Inherits Exception
        Public Sub New()
        End Sub

        Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            MyBase.New(info, context)
        End Sub

        Public Sub New(ByVal message As String)
        End Sub

        Public Sub New(ByVal message As String, ByVal innerException As Exception)
            MyBase.New(message, innerException)
        End Sub

        Public Sub DoIt()
            Throw Me
        End Sub
End Class

You can do a similar thing in C# with "throw this" but "Throw Me" sounds funnier :).

* Posted at 11.18.2010 06:22:09 PM CST | Link *

Blog History