handling exceptions

Discussion in 'Java' started by gaurav v bagga, Jan 22, 2007.

  1. hi all,
    i am having few methods they all throw few common exceptions

    method1 throws a,b,c
    method 2 throws a,b,c
    method3 throws a,b .......

    can i some how make it throw only one exception which will take care
    of res all others

    like

    method1 throws foo
    method2 throws foo

    and foo encapsulates all a,b,c

    something like that possible..

    a,b,c mainly i have are

    SQLException, InstantiationException, IllegalAccessException,
    ClassNotFoundException

    regards
    gaurav
     
    gaurav v bagga, Jan 22, 2007
    #1
    1. Advertising

  2. "gaurav v bagga" <> wrote in message
    news:...
    > hi all,
    > i am having few methods they all throw few common exceptions
    >
    > method1 throws a,b,c
    > method 2 throws a,b,c
    > method3 throws a,b .......
    >
    > can i some how make it throw only one exception which will take care
    > of res all others
    >
    > like
    >
    > method1 throws foo
    > method2 throws foo
    >
    > and foo encapsulates all a,b,c
    >
    > something like that possible..
    >
    > a,b,c mainly i have are
    >
    > SQLException, InstantiationException, IllegalAccessException,
    > ClassNotFoundException
    >
    > regards
    > gaurav
    >


    There is such an encapsulating exception type, namely Exception. You could
    catch Exception and where needed doing instanceof checks inside the catch to
    determine which exception was thrown. That will not win points for clarity
    and style but may be enough for what you need if you only require a
    "something i dont care about went wrong" signal.

    Remon
     
    Remon van Vliet, Jan 22, 2007
    #2
    1. Advertising

  3. gaurav v bagga wrote:
    > hi all,
    > i am having few methods they all throw few common exceptions
    >
    > method1 throws a,b,c
    > method 2 throws a,b,c
    > method3 throws a,b .......
    >
    > can i some how make it throw only one exception which will take care
    > of res all others
    >
    > like
    >
    > method1 throws foo
    > method2 throws foo
    >
    > and foo encapsulates all a,b,c
    >
    > something like that possible..
    >
    > a,b,c mainly i have are
    >
    > SQLException, InstantiationException, IllegalAccessException,
    > ClassNotFoundException
    >
    > regards
    > gaurav
    >


    You can do this by having foo extend Exception, but only use the
    constructors that have a Throwable cause parameter.

    However, it is desirable to make sure you provide enough granularity in
    foo and it subclasses to allow reasonable try-catch structure. It looks
    to me as though you have two major clusters of potential exceptions,
    database problems and class management problem. I would put those in
    separate subclasses of foo, so that a caller can catch them separately.

    Patricia
     
    Patricia Shanahan, Jan 22, 2007
    #3
    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. Raterus
    Replies:
    6
    Views:
    596
    Raterus
    Aug 24, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,126
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,064
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    635
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    713
Loading...

Share This Page