473,432 Members | 1,605 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,432 software developers and data experts.

handle exceptions

Dear all,

is it possible to handle 2 exception tipes in one block?

I.e

Try

if XXX then

throw new TypeA

else

throw new TypeB

end if

Catch x As TypeA

Doa

End Try

Catch x As TypeB

DoB

End Try
Nov 23 '05 #1
4 1239
"Boni" <oilia@nospam> schrieb:
is it possible to handle 2 exception tipes in one block?


You can use a generic exception type such as 'Exception' or
'ApplicationException' as type name in 'Catch ex As <TypeName>'. This
solution can be combined with a 'TypeOf' check inside the 'When' part of the
'Catch' statement:

\\\
Try
...
Catch ex As Exception _
Where _
TypeOf ex Is OverlowException OrElse _
TypeOf ex Is BadImageFormatException
...
Catch ex As Exception
...
End Try
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #2
Since you're defining the types of exceptions you can also do it in the
following way

Try
if XXX then
throw new TypeA
else
throw new TypeB
end if
Catch xA As TypeA
Doa
Catch xB As TypeB
DoB
End Try

Note that I removed the End Try you have in between the catches. Also
note that each catch has to have a different variable name, but that
shouldn't be an issue.
Catching multiple types of exceptions is extremely helpful when having
to handle specific types of exceptions. I particularly use it a lot
when I'm doing HTTP/HTTPS communications such as...
Try
'Do whatever code I need here to post to a website
Catch webEx as WebException
'Handle the web exception, knowing that it's an exception that
deals particularly with web based code.
Catch ex as Exception
'Handle all other types of exceptions, since every type of .NET
exception inherits from the generic Exception.
End Try

Nov 23 '05 #3
It is just what I was looking for.
Thanks a lot

"wooster11" <wo*******@yahoo.com> schrieb im Newsbeitrag
news:11**********************@g47g2000cwa.googlegr oups.com...
Since you're defining the types of exceptions you can also do it in the
following way

Try
if XXX then
throw new TypeA
else
throw new TypeB
end if
Catch xA As TypeA
Doa
Catch xB As TypeB
DoB
End Try

Note that I removed the End Try you have in between the catches. Also
note that each catch has to have a different variable name, but that
shouldn't be an issue.
Catching multiple types of exceptions is extremely helpful when having
to handle specific types of exceptions. I particularly use it a lot
when I'm doing HTTP/HTTPS communications such as...
Try
'Do whatever code I need here to post to a website
Catch webEx as WebException
'Handle the web exception, knowing that it's an exception that
deals particularly with web based code.
Catch ex as Exception
'Handle all other types of exceptions, since every type of .NET
exception inherits from the generic Exception.
End Try

Nov 23 '05 #4

"Boni" <oilia@nospam> wrote in message
news:Oa**************@TK2MSFTNGP14.phx.gbl...
is it possible to handle 2 exception tipes in one block?


Yes; catch a more "generic" Exception Type, then interrogate the
Exception you've caught to find out what it actually is.

.. . .
Catch ex as Exception
If TypeOf ex Is ArgumentException Then
. . .
End If

Note that you /can/ go down the road of defining your own,
custom Exceptions and inherit one from the other, something like :

Class myBasicException
Inherits ApplicationException
. . .
End Class

Class myNumberedException
Inherits myBasicException
. . .
Public Number as Integer
. . .
End Class

Class myOtherException
Inherits myBasicException
. . .
End Class

then

.. . .
Catch ex as myBasicException
If Typeof ex Is myNumberedException Then
ElseIf Typeof ex Is myOtherException Then
Else
Throw
End If
Catch ex as ApplicationException
. . .
Catch ex as Exception
. . .

HTH,
Phill W.
Nov 23 '05 #5

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

Similar topics

2
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to...
17
by: ahaupt | last post by:
Hi all, I'm currently writing a load of class libraries, but not the main application iteslf. I want to provide some method for reporting errors back to the main application. At the moment...
5
by: | last post by:
Unfortunately I've used exception handling as a debugging tool. Now I want to be smarter about handling errors. Today in the global.asx in the Application_OnError event, I inserted code to email...
3
by: chris | last post by:
hello, I can't seem to make this work: VS2005 I have a simple program that uses a backgroundworker control to execute a long process (webservice call) if that webservice call fails, i want to...
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
10
by: tcomer | last post by:
Hello! I've run into a problem and I can't seem to understand "why" the problem occurs. In my program: using System; using System.IO; using System.Xml; namespace MyNamespace { class MainApp
6
by: Author | last post by:
I am writing a small library in C#, which will be compiled to dll and shared by multiple applications. This is sorta new to me. I am wondering how professionals handle exceptions in a dll...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.