Results 1 to 6 of 6
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Question building a library - exception handling

    Hello,

    I'm working on building a library that will abstract database handling. I have read differing information about how to "properly" handle exceptions while constructing a library, but unfortunately am still a bit confused.

    Each database has the possibility to throw different exceptions based on whatever happens... Currently in my library I throw all of them so that the user of the library must deal with them somehow (or, swallow them...). My thoughts behind this were my library can't really recover from these exceptions since its a library to connect to databases and the connection failed for whatever reason... throwing the exception allows the user to handle it, possibly redirecting it through a logger of some sorts, or handling it and retrying the connection.

    However, there are several of the exceptions that I'm torn on simply handling it in my library in a try/catch block and just printing something like "you screwed up my beautiful properties file, now I can't connect!". This would provide a "clearer" more concise message to the library user to check their properties file for errors instead of just some exception like IllegalArgumentException which is a more cryptic way of saying the same thing.

    What is considered "best practice" when constructing a library intended to be used by more than just the original developer? Any reading materials you can point me to for learning about "proper" library setup would be much appreciated!

    thanks!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: building a library - exception handling

    Never swallow exceptions inside a library. Give the user of the library a chance to do something with them. And never print error messages. The user of the library may prefer to have them logged somewhere. Construct your exceptions with messages and let the user decide what to do with them.

    There are two schools of thought on checked vs. unchecked exceptions. Sun's recommendation was always to use checked exceptions. Lately I've been reading stuff advocating wrapping every checked exception in an unchecked exception. To me that's just plain crazy talk, but maybe there's something I don't get.
    Get in the habit of using standard Java naming conventions!

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: building a library - exception handling

    Thanks kjkrum for the thoughts...

    I ended up completing part of my library last night to where parts are usable (in rough state) -- so I wrote a test so I could use my own library, assess performance, and see how easy it was to use. It helped me realize there are a number of exceptions I should probably catch and rethrow as something more meaningful, for example:

    FileNotFoundException, SecurityException, and IOException can all be bundled and rethrown as a more generic "Can't Access Your Properties Files".

    While NumberFormatException and IllegalArgumentException could be cought and bundled to something new and more generic like "MalformedPropertiesException"

    Some things like ClassNotFoundException should probably not be thrown since its an exception that comes from one of my includes not working (supporting libraries)... how's the user supposed to know what to do in this situation? it likely means they should re-download the library or undelete that file...

    These were my thoughts at least after having to implement a try/multi-catch block with about 5 catches just to make a database connection (most exceptions are thrown from reading of properties file). It seemed excessive and not as informative as Id like it to be...

    I know there is heated debate around theories on exceptions... if anyone else has thoughts it would be much appreciated to open the discussion a bit, as this can sometimes be more constructive than simply reading about theory.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: building a library - exception handling

    Since this is a library I would suggest creating a hierarchy.
    That way a user can simply catch the generic <YourLibrary>Exception if they want to handle certain of your exceptions in the same way.

    Hibernate, for example, has a base HibernateException.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: building a library - exception handling

    Why not look at the Java API and source code for similar capabilities and see how Exceptions are handled there? And also notice where they are not handled and how that is documented.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: building a library - exception handling

    @Tolls,

    are you suggesting that I roundup my library internal exceptions into a base generic "LibraryException" that says something went wrong? Ths means the user of the library would have significantly fewer exceptions to worry about handling, and I suppose certain amounts of the possible exceptions it not reasonable to expect the user to handle during program execution (if properties file is not present or can't be accessed, then library should crash since there is no connection url data for the database). On the flip-side, this seems like it could be more confusing since it hides the true problem... I guess finding the "perfect balance" and applying it consistently is the challenge!

Similar Threads

  1. exception handling
    By Elaforestis in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 04:37 AM
  2. Exception handling
    By Kartiky14 in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 08:07 AM
  3. Exception handling
    By ExX in forum New To Java
    Replies: 6
    Last Post: 03-12-2012, 11:48 AM
  4. exception handling
    By niksipandit in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 02:25 PM
  5. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •