Classes with their own exception handling

P

Peter Aitken

It seems that some classes have their own exception handling built in. For
example if I try to use FileStream to open a file that does not exist, my
Try...Catch block seems to be ignored and instead I get an error message
that must be generated by the class itself. How can I tell which classes and
which exceptions do not require explicit exception handling code?
 
H

Herfried K. Wagner [MVP]

* "Peter Aitken said:
It seems that some classes have their own exception handling built in. For
example if I try to use FileStream to open a file that does not exist, my
Try...Catch block seems to be ignored and instead I get an error message
that must be generated by the class itself. How can I tell which classes and
which exceptions do not require explicit exception handling code?

In the case of the file stream, the messagebox is shown by Windows.
Sure, that's not documented.
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
It seems that some classes have their own exception handling built in.
This is more on a method (sub, function, property) level then a class level.
For
example if I try to use FileStream to open a file that does not exist, my
Try...Catch block seems to be ignored and instead I get an error message
that must be generated by the class itself.

The following works as excepted, the Catch block catches the
FileNotFoundException:

Try
' the FileNotFound.text file does not exist!
Dim stream As New System.IO.FileStream("FileNotFound.text",
System.IO.FileMode.Open)
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Open File Exception")
End Try


Can you post an example of using the FileStream that seems to ignore your
Try/Catch. As that sounds like a typo in your Try/Catch.
How can I tell which classes and
which exceptions do not require explicit exception handling code?
Short answer: None.
Long answer: The TreadAbortException is the only "special" exception that I
know of. Its special in that it is automatically rethrown at the end of a
Catch block, unless you use the Thread.ResetAbort method.

Hope this helps
Jay
 
P

Peter Aitken

Jay B. Harlow said:
Peter,
This is more on a method (sub, function, property) level then a class level.

The following works as excepted, the Catch block catches the
FileNotFoundException:

Try
' the FileNotFound.text file does not exist!
Dim stream As New System.IO.FileStream("FileNotFound.text",
System.IO.FileMode.Open)
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Open File Exception")
End Try


Can you post an example of using the FileStream that seems to ignore your
Try/Catch. As that sounds like a typo in your Try/Catch.

My mistake - it is not file not found but rather when a device is not ready
(CD drive with no disk in it). SOmeone else replied that WIndows pops up a
dialog before the exception can be caught.
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
That explains Herfried's comment. :)

In the case of OS specific dialogs, such as device not ready, they are
displayed by the OS way before the Framework has a chance to throw the
exception. In other words the OS is handling that specifically, the class
itself does not have any specific exception handling built in...

Hope this helps
Jay
 

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