Gettting the type of exception

  • Thread starter Thread starter Charles
  • Start date Start date
C

Charles

I have a component that throws various application
exceptions. I have a second component which is
responsible for alerting ops in the event of a major
problem. In order for it to be able to do this it needs
to be able to interrogate some custom properties on the
exception to diagnose the severity of the error. I can do
this by rethrowing the exception as follows:

Public Sub writeError(ByVal eException As
System.Exception


Try
Throw eException

Catch ex As MyAppException

If ex.Severity=1 then

End if

Catch ex as Excpetion

End try

Is there a neater way of doing this?
 
Charles,
I would use "Typeof obj Is Type". Which will preserve the stack trace:

TypeOf also matches derived types, "TypeOf ex Is MyAppException" will return
true for classes derived from MyAppException.
Public Sub writeError(ByVal eException As
System.Exception

If TypeOf eException Is MyAppException Then
Dim ex As MyAppException
ex = DirectCast(eException, MyAppException)
If ex.Severity=1 then

End if
Else

End If

Hope this helps
Jay
 
Back
Top