#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Location
    Dekalb, IL
    Posts
    63
    Rep Power
    0

    Question Error Handling in Custom DLL


    I'm creating a dll to use with my future .Net applications. I was wondering how exactly errors are handled or should be handled in a dll. By what I mean is that when an error occurs, do I just throw the error, popup a message box from the dll itself, or is there some other method that is proper?

    Thanks.
  2. #2
  3. More powar to you!
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    United Kingdom
    Posts
    699
    Rep Power
    210
    Depends upon what the class library (dll) is doing. For instance, if it's a class library with more forms and an error is thrown, then maybe a message box is appropriate. Some exceptions will and should cause your application to cease, since they can be completely unhandable.

    If it's a library offering business objects and business logic then it should probably throw the exception to the forms project (exe) and be handled there.

    It all depends what the library is doing, what the exception is. Circumstances and programmer choice, depending upon the situation at hand.

    Hope this helps.

    If you have a more specific enquiry, like an example, I may be able to offer some pointers.
    PHP Code:
    if($Tiredness TIREDNESS_MAX$CupOfTea++; 
    If you found what I've said helpful, I'm interested in reputation points!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Location
    Dekalb, IL
    Posts
    63
    Rep Power
    0
    Originally Posted by Tentuxius
    Depends upon what the class library (dll) is doing. For instance, if it's a class library with more forms and an error is thrown, then maybe a message box is appropriate. Some exceptions will and should cause your application to cease, since they can be completely unhandable.

    If it's a library offering business objects and business logic then it should probably throw the exception to the forms project (exe) and be handled there.

    It all depends what the library is doing, what the exception is. Circumstances and programmer choice, depending upon the situation at hand.

    Hope this helps.

    If you have a more specific enquiry, like an example, I may be able to offer some pointers.
    Yea, that makes sense. A more visual interface should display the error and a more backend type of solution should throw the exception.

    I guess one example would be, I'm creating a class to handle communication to a sql database, if an error occurs in this class, is that something I handle in the class itself or do I just throw the exception and let the higher levels deal with it.

    Here's kind of what I mean:

    Code:
    public SqlConnection GetConnection()
    {
       try
       {
          SqlConnection connection = new SqlConnection("connection string");
          connection.Open();
          return connection;
       }
       catch (SqlException ex)
       {
          // What should be done here?
       }
    }
    Just a method that establishes a connection to a sql database and returns it.

    Thanks again for the reply.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    296
    Rep Power
    65
    I would throw it since you won't be able to know whether it's being used in a windows forms app, console app, or web app.
  8. #5
  9. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    You could define a custom exception in your DLL/class and throw that, so it's very specific and you will know how to handle it. For instance, a new exception MyQueryException is more specific than SqlException, so you can more specifically handle it.

    Another option is to create a private member variable like string m_lastError and in your exception block assign a string message to that variable. Then define a public function like GetLastError() that returns m_lastError. (EDIT: this will only work if it's an instantiated class, not a static call to the class)

    Your function could also include a reference variable (pass a string to the function when you call it) and assign a value to that. After calling your GetConnection() function you could check the contents of the reference parameter.

    Some are better than others, but these are just some options. If you're creating a DLL to be used across multiple projects or applications you need to spend a good amount of time thinking about how you can best do this kind of exception handling... so great job for thinking about it beforehand!
    Discontent is the first necessity of progress. - Edison
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Location
    Dekalb, IL
    Posts
    63
    Rep Power
    0
    Originally Posted by eclipsed4utoo
    I would throw it since you won't be able to know whether it's being used in a windows forms app, console app, or web app.
    That makes sense.

    You could define a custom exception in your DLL/class and throw that, so it's very specific and you will know how to handle it. For instance, a new exception MyQueryException is more specific than SqlException, so you can more specifically handle it.
    So your saying create a custom exception for the class I'm working in. I understand that. So when an error occurs. I throw that custom exception I'm guessing that will contain the base exception that was originally thrown?

    Does anyone have an example?

    Thanks again guys. Just want to make sure I do this properly and neatly.
  12. #7
  13. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Originally Posted by Table16
    So your saying create a custom exception for the class I'm working in. I understand that. So when an error occurs. I throw that custom exception I'm guessing that will contain the base exception that was originally thrown?
    Yep. Create the custom exception in your DLL, make it public, and you can access it from the other app like catch(MyNamespace.MyClass.MyNewPublicException ex) and handle it from there. If you do inherit it from a SqlException you can always cast it as a SqlException to get the message, etc. I BELIEVE this is all right. Can someone else back me up?

    Great example here . In the throw statement I guess you can enter text for the message.

    Comments on this post

    • Table16 agrees : Thanks for the example. Exactly what I needed.
    Last edited by mateoc15; September 4th, 2009 at 08:58 AM.
    Discontent is the first necessity of progress. - Edison
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Location
    Dekalb, IL
    Posts
    63
    Rep Power
    0
    Originally Posted by mateoc15
    Yep. Create the custom exception in your DLL, make it public, and you can access it from the other app like catch(MyNamespace.MyClass.MyNewPublicException ex) and handle it from there. If you do inherit it from a SqlException you can always cast it as a SqlException to get the message, etc. I BELIEVE this is all right. Can someone else back me up?

    Great example here . In the throw statement I guess you can enter text for the message.
    That example was pretty much what I was looking for. Thanks alot!

IMN logo majestic logo threadwatch logo seochat tools logo