ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

TraverseFor

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

Source

<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")

Author: Brian Wigfield

Submitted on: 19 nov. 2010

Language: VB

Type: System.Exception

Views: 4100