|
-
Sep 5th, 2006, 08:53 AM
#1
Thread Starter
Frenzied Member
[resolved][C# 2.0] Exception Handling in a Class Library
I'm writing a class library and I'm trying to implement exception handling. On the catch block, what can you do with the Exception if the code is in a class library?
If it were a console program, I may want to view the exception with Console.WriteLn(e.Message) or if it were a Windows App then MessageBox.Show(e.Message) but I can't do both or choose in a class library.
Last edited by wey97; Sep 26th, 2006 at 07:31 AM.
-
Sep 5th, 2006, 09:00 AM
#2
Re: Exception Handling in a Class Library
Just include in the documentation that this Method may throw an exception and show what will be the reason if the exception is thrown.
Just like in VS2005 documentation
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Sep 5th, 2006, 09:21 AM
#3
Fanatic Member
Re: Exception Handling in a Class Library
Even if you do a Console.WriteLine(...) in a class library, it will print the description in the Output window. At least that way you don't have to leave the Catch() block empth, if that's what worrying you. Or is it something else you are suggesting? If you want to give the user of your class a choice then maybe you can put a node in App Config file to read it and then accordingly call an over-loaded method or maybe a switch..case to decide where the exceptions are logged.
-
Sep 5th, 2006, 09:24 AM
#4
Thread Starter
Frenzied Member
Re: Exception Handling in a Class Library
 Originally Posted by ComputerJy
Just include in the documentation that this Method may throw an exception and show what will be the reason if the exception is thrown.
Just like in VS2005 documentation
Could you give me an example of how to write my own simple handler?
-
Sep 5th, 2006, 09:26 AM
#5
Fanatic Member
Re: Exception Handling in a Class Library
What does this class library you're building do? If it's like the .NET framework libraries then you don't need any catch blocks unless you know how to handle the exception within your library. The application that uses your library should handle catching exceptions and logging them.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Sep 5th, 2006, 09:40 AM
#6
Thread Starter
Frenzied Member
Re: Exception Handling in a Class Library
 Originally Posted by DNA7433
What does this class library you're building do? If it's like the .NET framework libraries then you don't need any catch blocks unless you know how to handle the exception within your library. The application that uses your library should handle catching exceptions and logging them.
It's a wrapper library for some win32 api calls so for each call to an unsafe function, I felt I would need to "catch" any exceptions.
-
Sep 5th, 2006, 10:08 AM
#7
Fanatic Member
Re: Exception Handling in a Class Library
Ok, well think good practice would be to determine the exceptions that may be thrown by the API calls. Then create your own exception classes or use .NET exception classes that describe the error exactly, then throw your exceptions with the API exception as the inner exception. There's no need for logging in this scenario.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Sep 5th, 2006, 11:08 AM
#8
Thread Starter
Frenzied Member
Re: Exception Handling in a Class Library
So if I get an exception when I call an unsafe method in a library, will the exception "bubble up" to the calling program? In other words, is it not necessary to put exception handlers in your lib since the calling program can catch the exception?
Code:
// wrapper class lib
class Wrapper
{
void UnsafeCode(){ // may cause exception}
}
// Main program
Wrapper w = new Wrapper();
try
{
w.UnsafeCode();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
// or
Console.WriteLn(ex.Message);
}
Last edited by wey97; Sep 5th, 2006 at 11:13 AM.
-
Sep 5th, 2006, 11:41 AM
#9
Fanatic Member
Re: Exception Handling in a Class Library
Correct. Exceptions don't need to be caught (and in fact shouldn't) unless the code at that level knows how to handle the problem, or for such cases as logging. In your case I suppose you could within your library log exceptions thrown by unsafe methods but, as has been discussed above, what method would you use to log it? What if the caller doen't want that or wants something different? The answer is to just let the caller handle it. As ComputerJy has said just mention in the documentation for your wrapper methods that they may throw such-and-such an exception.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|