Subclassing an Exception

G

Guest

I have a generic catch block for exceptions of type Exception
Once inside the block I would like to know if my exception is of type
System.Data.SqlClient.SqlException. Actually I would like to pass the
variable of type Exception to a generic error handler, where I need to know
the specific Exception.
I need to find a something like 'Is Type' or something like that.
 
M

Mike Labosh

I have a generic catch block for exceptions of type Exception
Once inside the block I would like to know if my exception is of type
System.Data.SqlClient.SqlException. Actually I would like to pass the
variable of type Exception to a generic error handler, where I need to
know
the specific Exception.
I need to find a something like 'Is Type' or something like that.

How about this?

Private Sub genericErrorHandler(ByVal e As Exception)

Select Case e.GetType().Name
Case "SqlException"
Case "ArgumentNullException"
Case "IndexOutOfBoundsException"
Case "Exception"
End Select

End Sub

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
news:[email protected]...
 
H

Herfried K. Wagner [MVP]

Mike Labosh said:
Select Case e.GetType().Name
Case "SqlException"
Case "ArgumentNullException"
Case "IndexOutOfBoundsException"
Case "Exception"
End Select

\\\
Select Case True
Case TypeOf ex Is SqlException
...
Case TypeOf ex Is IndexOutOfBoundsException
...
...
End Select
///
 
M

Mike Labosh

Select Case True
Case TypeOf ex Is SqlException
...
Case TypeOf ex Is IndexOutOfBoundsException
...
...
End Select

Duh. Yup, you're right, that's cleaner.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
 
J

Jay B. Harlow [MVP - Outlook]

Arne,
In addition to the other comments I normally use multiple Catch clauses. As
its "cleaner", something like:

Try
DoSomething()
Catch ex As SystemException
' got a system exception
Catch ex As ApplicationException
' got a application exception
Catch ex As Exception
' got another kind of exception
End Try

| Actually I would like to pass the
| variable of type Exception to a generic error handler, where I need to
know
| the specific Exception.
In the above case I may call an overloaded error handler, something like:

Sub ErrorHandler(ex As SystemException)
Sub ErrorHandler(ex As ApplicationException)
Sub ErrorHandler(ex As Exception)


Alternatively you could use a If/ElseIf/Else, something like:

Try
DoSomething()
Catch ex As Exception
If TypeOf ex Is SystemException Then
' got a system exception
ElseIf TypeOf ex Is ApplicationException Then
' got a application exception
Else
' got another kind of exception
End If
End Try

Of course the If/ElseIf/Else could be in its own routine. For example my
"Sub ErrorHandler(ex As Exception)" might have the If/ElseIf/Else to call
Sub ErrorHandler(ex As SystemException) to handle the exception...

Remember on both to list derived exceptions before base exceptions. As the
first class that matches is the handler that will be used.

Hope this helps
Jay

|I have a generic catch block for exceptions of type Exception
| Once inside the block I would like to know if my exception is of type
| System.Data.SqlClient.SqlException. Actually I would like to pass the
| variable of type Exception to a generic error handler, where I need to
know
| the specific Exception.
| I need to find a something like 'Is Type' or something like that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top