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

Finding which exceptions a function throws

How do I know which exceptions a function throws? In my application, I make
a call to DirectoryInfo.GetDirectories("\\NetworkPath\Path") . I check for
System.IO.DirectoryNotFoundException and Security.SecurityException (which I
found in the Object Browser with VS2005). If Path is invalid, the block
catches System.IO.DirectoryNotFoundException as expected. However, if
NetworkPath is invalid, it instead throws a System.IO.IOException, which I
didn't see in the Object Browser. What documentation describes this behavior?
Jul 24 '08 #1
6 1070
Brian,

Unlike Java, .Net does not require that a method list all the possible
exceptions that it might throw.

As far as I know, your best bet is to always catch a plain old exception as
a backstop in case the ones you are explicitly handling are not in fact the
only ones that can get thrown.

Kerry Moorman
"Brian Nicholson" wrote:
How do I know which exceptions a function throws? In my application, I make
a call to DirectoryInfo.GetDirectories("\\NetworkPath\Path") . I check for
System.IO.DirectoryNotFoundException and Security.SecurityException (which I
found in the Object Browser with VS2005). If Path is invalid, the block
catches System.IO.DirectoryNotFoundException as expected. However, if
NetworkPath is invalid, it instead throws a System.IO.IOException, which I
didn't see in the Object Browser. What documentation describes this behavior?
Jul 24 '08 #2
On Jul 24, 3:04*pm, Brian Nicholson
<BrianNichol...@discussions.microsoft.comwrote:
How do I know which exceptions a function throws? *In my application, Imake
a call to DirectoryInfo.GetDirectories("\\NetworkPath\Path") . *I check for
System.IO.DirectoryNotFoundException and Security.SecurityException (which I
found in the Object Browser with VS2005). *If Path is invalid, the block
catches System.IO.DirectoryNotFoundException as expected. *However, if
NetworkPath is invalid, it instead throws a System.IO.IOException, which I
didn't see in the Object Browser. *What documentation describes this behavior?
I have noticed that many of the comments for the class members list
the exceptions that it might throw, but even if it does I wouldn't
trust it as fool proof.

Also, the general rule of thumb is to not catch an exception you can't
do something with. With that said, if you don't plan on telling your
user exactly what happened (or doing something else with the different
types) then I would say to just go with "Catch ex As Exception"

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 24 '08 #3
>
I have noticed that many of the comments for the class members list
the exceptions that it might throw, but even if it does I wouldn't
trust it as fool proof.

Also, the general rule of thumb is to not catch an exception you can't
do something with. With that said, if you don't plan on telling your
user exactly what happened (or doing something else with the different
types) then I would say to just go with "Catch ex As Exception"

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Thanks for the tip -- I was asking because I was trying to catch all the
exceptions and display user-friendly messages instead of the default ones
inside of each catch block. Is there a better way to go about doing this?
Jul 24 '08 #4
Is there a better way to go about doing this?

Not really.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Jul 24 '08 #5
Brian Nicholson wrote:
How do I know which exceptions a function throws? In my application,
I make a call to DirectoryInfo.GetDirectories("\\NetworkPath\Path") .
I check for System.IO.DirectoryNotFoundException and
Security.SecurityException (which I found in the Object Browser with
VS2005). If Path is invalid, the block catches
System.IO.DirectoryNotFoundException as expected. However, if
NetworkPath is invalid, it instead throws a System.IO.IOException,
which I didn't see in the Object Browser.
How about checking for Directory.Exists first?

Andrew
Jul 25 '08 #6
Brian,
I know this won't hide the user from the slightly unfriendly messages you
get from VB.NET but it might produce slightly tidier results for those
unexpected errors:

In your main module or at the top of your form declare a static variable as
follows:
Public Const Support As String = "Your Name and Contact Number"
Public Const AdminContact As String = "User Admin Name"
Public Const NNL As String = vbCrLf & vbCrLf

Public Sub PEH(ByVal ProcedureName As String, ByVal FormName As String,
ByVal ErrorMessage As String)
Dim Msg As String = "WARNING: An error has occured in the " &
ProcedureName & " procedure in the "

Try

Msg += FormName & " form or module. The error message returned
is as follows:" & NNL
Msg += ErrorMessage & NNL
Msg += "Please make a note of the above error and advise " &
AdminContact & ", who will "
Msg += "advise you what to do."
MessageBox.Show(Msg, H, MessageBoxButtons.OK,
MessageBoxIcon.Information)

Catch ex As Exception

Msg = "WARNING: An error has occurred in the error handler
itself??" & NNL
Msg += "Please contact " & Support & " as this shouldn't happen
normally!" & NNL
Msg += "Click ""OK"" to end the application."
MessageBox.Show(Msg, H, MessageBoxButtons.OK,
MessageBoxIcon.Information)
End

End Try

End Sub

In your individual catch blocks within Subs and Functions that might throw
unexpected errors do this:
Catch ex As Exception

PEH("NameOfSuborFunc", "NameOfFormOrModule", ex.Message)
End Try

If the developer changes you can change the Const for Support and everwhere
the error handler occurs will automatically quote the new deveoper and also
if your client who uses the application changes who is the administrator you
just change the AdminContact and likewise their details change automatically
everywhere.

Hope this is of some use.

Siv

--
Martley, Near Worcester, UK
"Brian Nicholson" wrote:

I have noticed that many of the comments for the class members list
the exceptions that it might throw, but even if it does I wouldn't
trust it as fool proof.

Also, the general rule of thumb is to not catch an exception you can't
do something with. With that said, if you don't plan on telling your
user exactly what happened (or doing something else with the different
types) then I would say to just go with "Catch ex As Exception"

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/

Thanks for the tip -- I was asking because I was trying to catch all the
exceptions and display user-friendly messages instead of the default ones
inside of each catch block. Is there a better way to go about doing this?
Jul 26 '08 #7

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

Similar topics

26
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
15
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
0
RedSon
by: RedSon | last post by:
Chapter 2: How to handle Exceptions When you call a program, sometimes you get a message about an Unhandled exception type. This means, that something throws (or might throw) an Exception, but the...
0
Nepomuk
by: Nepomuk | last post by:
Chapter 1: What is an Exception? Imagine, you want to write a program for calculating. You start, it works fine, you can add, subtract, multiply and divide. Then there's a question: What about the...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
4
by: datastrc | last post by:
Hello When compiling the following example #include <cstdlib> //declarations of malloc and free #include <new> #include <iostream> using namespace std; class C {
22
by: ben mitch | last post by:
Hi I hope you'll see this cross-post (c/c++) as appropriate. I also admit immediately that the question *may* turn out to be compiler-/os-specific, in which case I apologize. But I wonder if...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.