exception handling patterns?

Discussion in 'Java' started by Timasmith, Oct 17, 2006.

  1. Timasmith

    Timasmith Guest

    Hi,

    Does anyone know of some good articles on exception handling. From my
    own experience this is where I am at:

    System (as in the application) defined exceptions should

    a) Extend something like a system.lang.xxxException when you want to
    defend but you dont want to force the method user to catch/throw the
    exception
    e.g. NullArgumentException extends NullPointerException

    b) Extend Exception when you need the method user to handle the
    specific exception
    e.g. InvalidUserNameException extends Exception

    c) Extend your own exceptions when you have a commonality that the
    user could protect against
    e.g. SQLException extends DataAccessException
    e.g. DataParameterException extends DataAccessException

    Not sure what else to do around Exception Handling other than handle
    them gracefully.
    Timasmith, Oct 17, 2006
    #1
    1. Advertising

  2. Timasmith

    Oliver Wong Guest

    "Timasmith" <> wrote in message
    news:...
    > Hi,
    >
    > Does anyone know of some good articles on exception handling. From my
    > own experience this is where I am at:
    >
    > System (as in the application) defined exceptions should
    >
    > a) Extend something like a system.lang.xxxException when you want to
    > defend but you dont want to force the method user to catch/throw the
    > exception
    > e.g. NullArgumentException extends NullPointerException
    >
    > b) Extend Exception when you need the method user to handle the
    > specific exception
    > e.g. InvalidUserNameException extends Exception


    Your rule "A" should be changed so that the exceptions of interest
    extend RuntimeException, or one of its subclasses. NullPointerException is a
    subclass of RuntimeException, so the example still works.

    Similarly, in "B" you could extend any exception which is not a
    RuntimeException (Exception itself is an exception, but not a runtime
    exception).

    Also, don't be afraid to occasionally throw one of Sun's built-in
    exceptions, instead of defining your own.

    - Oliver
    Oliver Wong, Oct 17, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crichmon
    Replies:
    4
    Views:
    471
    Mabden
    Jul 7, 2004
  2. Replies:
    3
    Views:
    969
    Al Balmer
    Jan 11, 2008
  3. James L
    Replies:
    1
    Views:
    397
    Mark Space
    Mar 5, 2008
  4. Peter
    Replies:
    34
    Views:
    1,919
    James Kanze
    Oct 17, 2009
  5. snarf
    Replies:
    7
    Views:
    99
Loading...

Share This Page