Results 1 to 9 of 9

Thread: [resolved][C# 2.0] Exception Handling in a Class Library

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved [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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Exception Handling in a Class Library

    Quote 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?

  5. #5
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Exception Handling in a Class Library

    Quote 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.

  7. #7
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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.

  9. #9
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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
  •  



Click Here to Expand Forum to Full Width