handling exceptions

G

gaurav v bagga

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
 
R

Remon van Vliet

gaurav v bagga said:
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
 
P

Patricia Shanahan

gaurav said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top