• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapping exceptions within a common exception.

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just want to get people's view upon the following. If wrapping exceptions within a common exception, for example, ,
would people:
(a)Wrap all exceptions that do occur which are subclasses of in the
or
(b) For all exceptions that can occur in a remote sense, for example a wrap these in a because they can happen remotely.
Just wondering people's view upon wrapping exceptions within a common exception, and which is right, (a) or (b)?
Thanks.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

I wrapped the NotBoundException in a RemoteException.

But of course it depends on the situation you are in, the interface you have to implement,... wrapping exception in another exception is not always a good idea. For example wrapping an IllegalArgumentException or an ArrayIndexOutOfBoundsException in a RemoteException or IOException makes no sense at all.

Kind regards,
Roel
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is usually no need to wrap a subclass of an exception in another instance of that exception, as it already is an instance of that exception.

E.g. There is no value in code such as

In this case, ConnectException is a subclass of RemoteException, and as such the entire try...catch block and the subsequent re-throwing is redundant - without the try...catch block the ConnectException would have been thrown from the doSomething method.

The only time you might want to wrap it and re-throw it is if you are adding information. Such as:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic