473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Subclassing an Exception

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.
Nov 21 '05 #1
4 1423
>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"
"Arne" <Ar**@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
Nov 21 '05 #2
"Mike Labosh" <ml*****@hotmail.com> schrieb:
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 S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #3
> 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"
Nov 21 '05 #4
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

"Arne" <Ar**@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
|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.
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Chris Cioffi | last post by:
Hello all! I'm trying to subclass int such that once it reaches a certain value, it flips back to the starting count. This is for use as an X12 control number. Subclassing int and getting the...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
2
by: Alexander Wehrli | last post by:
Hi, I need to write a functionality to catch all Windows Messages from any Window (like spy++ does). I thought that would be possible by subclassing the window. So I wrote a class that...
1
by: Chuck Haeberle | last post by:
We have need to share functionality across all of our application web pages, so we decided to subclass from System.Web.UI.Page. When we create a new aspx, the Visual Studio designer automatically...
7
by: Simon | last post by:
I've struggled manfully and have finally managed to subclass the Common Dialog class to add my own controls. My last problem is how to set the lpfnHook attribute of the OPENFILENAME structure....
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.