PC Review


Reply
Thread Tools Rate Thread

How can I determine WHICH exception I got in my CATCH?

 
 
l.woods
Guest
Posts: n/a
 
      30th Mar 2005
I want to set up my CATCH for a specific exception, but I really don't know
which one of the multitude that it is. I am getting the exception now with

Catch ex as Exception

but I want to be more specific. I can't find any property of the exception
object that tells me WHICH one it is.

TIA,

Larry Woods


 
Reply With Quote
 
 
 
 
Larry Lard
Guest
Posts: n/a
 
      30th Mar 2005

l.woods wrote:
> I want to set up my CATCH for a specific exception, but I really

don't know
> which one of the multitude that it is. I am getting the exception

now with
>
> Catch ex as Exception
>
> but I want to be more specific. I can't find any property of the

exception
> object that tells me WHICH one it is.


Its type.

If TypeOf ex Is SpecificExceptionIWant Then
....

Concrete example:

Dim a As Integer = 1, b As Integer = 0, c As Integer

Try
c = a \ b
Catch ex As Exception
If TypeOf ex Is DivideByZeroException Then
MsgBox("No surprise")
Else
MsgBox("Something WEIRD")
End If
End Try

This is exactly like looking for specific Err.Number's in VB6, which is
what I suspect you are looking for.


--
Larry Lard
Replies to group please

 
Reply With Quote
 
Phill. W
Guest
Posts: n/a
 
      30th Mar 2005
"l.woods" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I want to set up my CATCH for a specific exception,


You can explicitly catch any sort of Exception you want, as in

Catch nrx as NullReferenceException
Catch ax as ArgumentException
Catch ex as Exception

but the "trick" is catch the "smallest" one first - When an Exception
occurs, VB will use the /most appropriate/ exception handler.

> but I really don't know which one of the multitude that it is.


Ah.

> I can't find any property of the exception object that tells me
> WHICH one it is.


That's because there isn't one - you can simply examine the Type
of the Exception object directly:

Catch ex as Exception
If TypeOf ex Is ArgumentException Then
DirectCast( ex, ArgumentException).thingamydoodle
End If

HTH,
Phill W.


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      30th Mar 2005
"l.woods" <(E-Mail Removed)> schrieb:
>I want to set up my CATCH for a specific exception, but I really don't know
> which one of the multitude that it is. I am getting the exception now
> with
>
> Catch ex as Exception
>
> but I want to be more specific. I can't find any property of the
> exception
> object that tells me WHICH one it is.


\\\
If TypeOf ex Is FooException Then
...
ElseIf TypeOf ex Is GooException Then
...
....
End If
///

- or -

\\\
Select Case True
Case TypeOf ex Is FooException
...
Case TypeOf ex Is GooException
...
...
End Select
///

Note that FxCop will complain about 'Catch' blocks which catch the generic
exception type. However, this rule is controversial and may be
altered/removed. German article on this issue:

Mythos: Catch( Exception) ist böse
<URL:http://www.die.de/blog/PermaLink.aspx?guid=c0d9a5d0-b12d-4995-8447-94040a932dc9>

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

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      30th Mar 2005
Larry,
In addition to Larry's sample of:

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

I prefer:

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

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


I prefer the second, as its 'cleaner' and it allows you to only catch the
specific exception you want, while ignoring unwanted exceptions. For
example:

Try
DoSomething()
Catch ex As FileNotFoundException
' got a file not found exceptoin
End Try

Will only catch FileNotFoundExceptions, other exceptions will continue
upward to another exception handler...

Another useful tidbit to limit what exceptions are caught is the When
clause. For example:

Dim request As HttpWebRequest
Dim response As HttpWebResponse
Try
response = DirectCast(request.GetResponse(), HttpWebResponse)
Catch ex As WebException When TypeOf ex.Response Is HttpWebResponse
response = DirectCast(ex.Response, HttpWebResponse)
End Try

The catch block will only handle WebExceptions that have a Reponse type of
HttpWebResponse.

Hope this helps
Jay


"l.woods" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
|I want to set up my CATCH for a specific exception, but I really don't know
| which one of the multitude that it is. I am getting the exception now
with
|
| Catch ex as Exception
|
| but I want to be more specific. I can't find any property of the
exception
| object that tells me WHICH one it is.
|
| TIA,
|
| Larry Woods
|
|


 
Reply With Quote
 
Crouchie1998
Guest
Posts: n/a
 
      30th Mar 2005
Try
....
Catch ex As Exception

MessageBox.Show(ex.ToString)

End Try

That will tell you exactly which type of exception you have then you can
catch the exceptions more precisely.

Try
....
Catch ex As IO.FileIOException
' Handle file access error
Catch exx As Exception
' Handle all other errors
End Try

You can also use 'IndexOf' too if you know the exact error message. 'Example
Only' below:

Dim sr As IO.StreamReader

Try
sr = New IO.StreamReader("C:\zzzzz.txt")
sr.Read()
Catch ex As Exception
If ex.ToString.IndexOf("Could not find file") > 0 Then
MessageBox.Show("File Not Found")
End If
End Try

If Not sr Is Nothing Then sr.Close()


 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      30th Mar 2005
Crouchie,
| You can also use 'IndexOf' too if you know the exact error message.
'Example
| Only' below:
I would not recommend using this approach as it does not localize very well.

Hope this helps
Jay


"Crouchie1998" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Try
| ...
| Catch ex As Exception
|
| MessageBox.Show(ex.ToString)
|
| End Try
|
| That will tell you exactly which type of exception you have then you can
| catch the exceptions more precisely.
|
| Try
| ...
| Catch ex As IO.FileIOException
| ' Handle file access error
| Catch exx As Exception
| ' Handle all other errors
| End Try
|
| You can also use 'IndexOf' too if you know the exact error message.
'Example
| Only' below:
|
| Dim sr As IO.StreamReader
|
| Try
| sr = New IO.StreamReader("C:\zzzzz.txt")
| sr.Read()
| Catch ex As Exception
| If ex.ToString.IndexOf("Could not find file") > 0 Then
| MessageBox.Show("File Not Found")
| End If
| End Try
|
| If Not sr Is Nothing Then sr.Close()
|
|


 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      31st Mar 2005
Jay, this has no relevance to this thread but I noted that sr was returned by
VB.Net as nothing as are other VB.Net varibles that can't be initialized.
This got me into the habit of returning nothing for reference type variables
from some of my routines when something couldn't be found. For example, I do
some work with reading and manipulating ID3 tags from mp3 files and when my
routines can't find a tag item, the string is returned as nothing. However,
in every tag, I must check for either the tag isn't there or the text
associated with the tag is "". This is what made me wish that things like
String.trim(x) would just return nothing when x is nothing!

"Jay B. Harlow [MVP - Outlook]" wrote:

> Crouchie,
> | You can also use 'IndexOf' too if you know the exact error message.
> 'Example
> | Only' below:
> I would not recommend using this approach as it does not localize very well.
>
> Hope this helps
> Jay
>
>
> "Crouchie1998" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> | Try
> | ...
> | Catch ex As Exception
> |
> | MessageBox.Show(ex.ToString)
> |
> | End Try
> |
> | That will tell you exactly which type of exception you have then you can
> | catch the exceptions more precisely.
> |
> | Try
> | ...
> | Catch ex As IO.FileIOException
> | ' Handle file access error
> | Catch exx As Exception
> | ' Handle all other errors
> | End Try
> |
> | You can also use 'IndexOf' too if you know the exact error message.
> 'Example
> | Only' below:
> |
> | Dim sr As IO.StreamReader
> |
> | Try
> | sr = New IO.StreamReader("C:\zzzzz.txt")
> | sr.Read()
> | Catch ex As Exception
> | If ex.ToString.IndexOf("Could not find file") > 0 Then
> | MessageBox.Show("File Not Found")
> | End If
> | End Try
> |
> | If Not sr Is Nothing Then sr.Close()
> |
> |
>
>
>

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      31st Mar 2005
Dennis,
| Jay, this has no relevance to this thread
I take it you mean your comments has no relevance to which exception. ;-)

| but I noted that sr was returned by
| VB.Net as nothing
What is "sr returned by VB.NET"?

| as are other VB.Net varibles that can't be initialized.
All VB.NET variables can be initialized! can you give me an example of one
that cannot?

| This got me into the habit of returning nothing for reference type
variables
| from some of my routines when something couldn't be found.
Yes returning Nothing is handy sometimes, returning a "NullObject" is
usually handier, aka Special Case pattern.
http://www.martinfowler.com/eaaCatalog/specialCase.html

| For example, I do
| some work with reading and manipulating ID3 tags from mp3 files and when
my
| routines can't find a tag item, the string is returned as nothing.
Do you need to know specifically if its not found? If I don't specifically
need to know I will return String.Empty rather then Nothing, allowing me to
use instance methods on the string as normal. I would consider throwing an
exception for not found, especially if not found does not allow me to
continue. I would consider returning Nothing if I needed to know
specifically, but would then rather quickly change it to String.Empty to
continue processing... I would consider using ByRef parameters to return a
non-Nothing string & an boolean indicator if its found or not, however this
feels like returning an object other then string (such as a ID3Tag class
that I defined) that encapsulated the found string or String.Empty the fact
none was found.

Hope this helps
Jay


"Dennis" <(E-Mail Removed)> wrote in message
newsC100983-E789-4A83-B172-(E-Mail Removed)...
| Jay, this has no relevance to this thread but I noted that sr was returned
by
| VB.Net as nothing as are other VB.Net varibles that can't be initialized.
| This got me into the habit of returning nothing for reference type
variables
| from some of my routines when something couldn't be found. For example, I
do
| some work with reading and manipulating ID3 tags from mp3 files and when
my
| routines can't find a tag item, the string is returned as nothing.
However,
| in every tag, I must check for either the tag isn't there or the text
| associated with the tag is "". This is what made me wish that things like
| String.trim(x) would just return nothing when x is nothing!
|
<<xnip>>


 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      1st Apr 2005
Thanks for your comments. I"m just a hobbiest with VB.Net so I'm sure your
points are valid for Pros.

"Jay B. Harlow [MVP - Outlook]" wrote:

> Dennis,
> | Jay, this has no relevance to this thread
> I take it you mean your comments has no relevance to which exception. ;-)
>
> | but I noted that sr was returned by
> | VB.Net as nothing
> What is "sr returned by VB.NET"?
>
> | as are other VB.Net varibles that can't be initialized.
> All VB.NET variables can be initialized! can you give me an example of one
> that cannot?
>
> | This got me into the habit of returning nothing for reference type
> variables
> | from some of my routines when something couldn't be found.
> Yes returning Nothing is handy sometimes, returning a "NullObject" is
> usually handier, aka Special Case pattern.
> http://www.martinfowler.com/eaaCatalog/specialCase.html
>
> | For example, I do
> | some work with reading and manipulating ID3 tags from mp3 files and when
> my
> | routines can't find a tag item, the string is returned as nothing.
> Do you need to know specifically if its not found? If I don't specifically
> need to know I will return String.Empty rather then Nothing, allowing me to
> use instance methods on the string as normal. I would consider throwing an
> exception for not found, especially if not found does not allow me to
> continue. I would consider returning Nothing if I needed to know
> specifically, but would then rather quickly change it to String.Empty to
> continue processing... I would consider using ByRef parameters to return a
> non-Nothing string & an boolean indicator if its found or not, however this
> feels like returning an object other then string (such as a ID3Tag class
> that I defined) that encapsulated the found string or String.Empty the fact
> none was found.
>
> Hope this helps
> Jay
>
>
> "Dennis" <(E-Mail Removed)> wrote in message
> newsC100983-E789-4A83-B172-(E-Mail Removed)...
> | Jay, this has no relevance to this thread but I noted that sr was returned
> by
> | VB.Net as nothing as are other VB.Net varibles that can't be initialized.
> | This got me into the habit of returning nothing for reference type
> variables
> | from some of my routines when something couldn't be found. For example, I
> do
> | some work with reading and manipulating ID3 tags from mp3 files and when
> my
> | routines can't find a tag item, the string is returned as nothing.
> However,
> | in every tag, I must check for either the tag isn't there or the text
> | associated with the tag is "". This is what made me wish that things like
> | String.trim(x) would just return nothing when x is nothing!
> |
> <<xnip>>
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catch(Exception ex) Vs catch (Exception) =?Utf-8?B?Y2FzaGRlc2ttYWM=?= Microsoft C# .NET 17 16th Oct 2006 04:38 PM
Difference between try{}catch{} and try{}catch(Exception e){} ? Mr Flibble Microsoft C# .NET 7 22nd Jun 2006 04:25 PM
Help, I can't catch this exception =?Utf-8?B?Wm9ycGllZG9tYW4=?= Microsoft VB .NET 0 23rd Aug 2005 03:53 PM
Cant catch an exception =?Utf-8?B?VHJpcw==?= Microsoft Dot NET 2 8th Jul 2005 05:00 PM
about catch exception Ha ha Microsoft C# .NET 1 18th Dec 2004 02:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:55 PM.