For a library, your main concern should be giving the programmer sufficient information.
In many cases, the exception thrown by the runtime may be good enough, and you can just let the exception fly. In other cases, you may want to wrap the exception in your own exception to provide additional contextual information. *However*, make sure that you don't make things worse for the programmer: try { // lots of stuff here } catch (Exception e) { throw new MyException("Error processing query", e); } Why is this bad? Well, if the code we're using could thow a FileNotFoundException and the user might like to treat that differently than other exceptions, they'd have to write: Try { // call your routine { Catch (MyException e) { if (e.InnerException.GetType() == typeof(FileNotFoundException) { // do something } // do something else } That's really bad. Not only did the user have to write code, but it's really easy for them to accidentally swallow exceptions. My code at "do something else" should do a "throw"... My advice is to be pretty careful with what you do in your library. A library with no try-catches in it performs correctly, and it's easy to make things worse by adding stuff like above. -----Original Message----- From: Sam Gentile [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 28, 2002 12:38 PM To: [EMAIL PROTECTED] Subject: [DOTNET] 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? --------------------- Sam Gentile .NET Consultant Co-author: Wrox Visual C++ .NET: A primer for C++ developers BLOG: http://radio.weblogs.com/0105852/ http://www.project-inspiration.com/sgentile/DotNet.htm http://www.project-inspiration.com/sgentile/ --------------------------- _________________________________________________________________ Chat with friends online, try MSN Messenger: http://messenger.msn.com You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.