Hi Sam,

In your example, I'd say it doesn't make much sense to let the
ArgumentNullException out unless the null argument originated outside of
your library, in which case you'd probably want to catch the exception
and throw a new ArgumentNullException describing the argument from the
callers perspective. Otherwise you'd need to catch it and throw a
different exception as you say... The guidelines in the SDK (worth a
read) suggest that you only define your own ApplicationException-derived
classes when you think the caller may need to catch that particular
exception, otherwise you can just throw an ApplicationException.

I suppose the trick is to look at your library from the caller's point
of view. What exceptions would you expect, and what exceptions would you
find useful? If you let an exception out that doesn't tell the caller
anything useful, then it's probably not the right exception to be
letting out. At the very least, throw an ApplicationException with a
useful message (the inner exception also proves handy here).

HTH,
G.


-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Sam Gentile
Sent: 28 May 2002 20:38
To: [EMAIL PROTECTED]
Subject: Need a good real-world C# Exceptions Resource or Stategy for
Library


I have just been poring through C# books and no one seems to address
this
well. Sure, otherwise great books like Jesse Liberty's "Progarmming C#"
talk
about Exceptions but all the examples are too trivial. The examples just
shoot out an "I am here" kind of thing in the catch handler. Actually
some
C# books don't even discuss it!! I need something more.

Lets suppose I am/have creating a C# library of classes in a namespace.
I
want to put in full exception handling. I have some circumstances where
I am
creating files and reading them with System.IO classes, I am doing a lot
of
things with System.Xml. The thing is that I want to define and implement
a
sound exception processing strategy. Obviously, I don't really want to
handle exceptions in a library by putting up
System.Console.WriteLine("Can't
open file"). I want to throw them up to the caller, but what? Suppose I
catch an ArgumentNullException on a FileStream constructiuon. Does it
make
sense to define my custom exceptions and throw those up? Are there any
good
C# resources that show real exception processing/good pratices?

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to