Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   handling exceptions (http://www.velocityreviews.com/forums/t390270-handling-exceptions.html)

gaurav v bagga 01-22-2007 02:39 PM

handling exceptions
 
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


Remon van Vliet 01-22-2007 03:30 PM

Re: handling exceptions
 

"gaurav v bagga" <gaurav.v.bagga@gmail.com> wrote in message
news:1169476785.128513.159720@s34g2000cwa.googlegr oups.com...
> 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



Patricia Shanahan 01-22-2007 03:35 PM

Re: handling exceptions
 
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


All times are GMT. The time now is 02:02 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.