473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding which exceptions a function throws

How do I know which exceptions a function throws? In my application, I make
a call to DirectoryInfo.G etDirectories(" \\NetworkPath\P ath"). I check for
System.IO.Direc toryNotFoundExc eption and Security.Securi tyException (which I
found in the Object Browser with VS2005). If Path is invalid, the block
catches System.IO.Direc toryNotFoundExc eption as expected. However, if
NetworkPath is invalid, it instead throws a System.IO.IOExc eption, which I
didn't see in the Object Browser. What documentation describes this behavior?
Jul 24 '08 #1
6 1084
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.G etDirectories(" \\NetworkPath\P ath"). I check for
System.IO.Direc toryNotFoundExc eption and Security.Securi tyException (which I
found in the Object Browser with VS2005). If Path is invalid, the block
catches System.IO.Direc toryNotFoundExc eption as expected. However, if
NetworkPath is invalid, it instead throws a System.IO.IOExc eption, 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.mi crosoft.comwrot e:
How do I know which exceptions a function throws? *In my application, Imake
a call to DirectoryInfo.G etDirectories(" \\NetworkPath\P ath"). *I check for
System.IO.Direc toryNotFoundExc eption and Security.Securi tyException (which I
found in the Object Browser with VS2005). *If Path is invalid, the block
catches System.IO.Direc toryNotFoundExc eption as expected. *However, if
NetworkPath is invalid, it instead throws a System.IO.IOExc eption, 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.G etDirectories(" \\NetworkPath\P ath").
I check for System.IO.Direc toryNotFoundExc eption and
Security.Securi tyException (which I found in the Object Browser with
VS2005). If Path is invalid, the block catches
System.IO.Direc toryNotFoundExc eption as expected. However, if
NetworkPath is invalid, it instead throws a System.IO.IOExc eption,
which I didn't see in the Object Browser.
How about checking for Directory.Exist s 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, MessageBoxButto ns.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, MessageBoxButto ns.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("NameOfSubo rFunc", "NameOfFormOrMo dule", 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
2920
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 belief is that checked exceptions should be required in .NET. I find that many others share the same views as I do. It is extremely frustrating to have to work around this with hacks like Abstract ADO.NET and CLRxLint (which still don't solve the...
9
2342
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 with unmaintainable code (an so are now not using exceptions). My position is that "I can be convinced to use exceptions" and my experience was that it let to code that was (much) more difficult to debug. The team decided that we'd give...
15
426
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 have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %
1
2390
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 look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
0
5336
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 compiler doesn't know, what to do then. You can deal with this in two different ways: Throw the Exception further (pass it on to some other place) Catch the Exception and cope with it Depending on what you want to do, when an Exception occurs,...
0
3839
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 division by zero? Say, your division method looks like this: public double div(double a, double b) { return a/b; } You try:
0
6504
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 NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception. In general, you can sort Exceptions into two groups: Checked and unchecked Exceptions. Checked Exceptions are checked by the compiler at compilation time. Most...
4
4942
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
2606
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 there's an underlying truth. We are writing a (cross-platform) 'framework' application (in C++, as it happens) that allows users to author 'plugins' (as shared library modules). Since the lifetime of plugins is expected to be long, and the
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.