TraverseFor

    <System.Runtime.CompilerServices.Extension()> _
    Public Function TraverseFor(Of T As Class)(ByVal ex As Exception) As T

        If ex Is Nothing Then _
            Return Nothing

        If ex.GetType Is GetType(T) Then _
            Return TryCast(ex, T)

        Return TryCast(ex.InnerException.TraverseFor(Of T)(), T)

    End Function
Example:
        If Server.GetLastError().TraverseFor(Of SessionNAException)() IsNot Nothing Then _
            Response.Redirect("/Error/SessionUnavailable")

Description

Returns the first inner exception you are looking for or null if not found.

Details

Double click on the code to select all.

 

;