Getting Exception wrapped in an exception

M

Manish Hatwalne

I have a legacy code (and I can't change this part) which uses construct
such as -

try{
doSomething();
.........
}catch(Exception e){
throw new CustomException(e);
}

Now while using call to this method, I have to handle this exception, and I
need to write specific message meant for an exception wrapped by this
CustomException. So I write -

try{
callThisMethod();
}catch(CustomException ce){
Throwable t = ce.getCause() ; //what should I use here??
if(t instanceof SomeException){
/// appropriate message
}
}

My problem is how do I get the exception wrapped by this CustomException? I
thought getCause() will return me that, but it returns CustomException. How
do I get the wrapped exception? It could range from NoRouteToHostException
to a NullPointerException, and I need to have my messages based on that. How
do I do this?

TIA,
- Manish
 
S

Stefan Waldmann

Manish said:
I have a legacy code (and I can't change this part) which uses construct
such as -

try{
doSomething();
.........
}catch(Exception e){
throw new CustomException(e);
}
[snip]

Why would you just catch a generic Exception? Normally when you use try
/ catch, you know which kind of Exception you are catching. So you could
create your CustomException there with the appropriate message.
For example:

try {
doDatabaseAccess();
}
catch(SQLException se) {
throw new CustomException("Database access failed.", se);
}

or

try {
String str = "123xy";
int i = Integer.parseInt(str);
catch(NumberFormatException nfe) {
throw new CustomException(str+" could not be parsed to an int.",nfe);
}

So you have your appropriate message already in your CustomException and
when necessary can get it by

catch(CustomException ce) {
String msg = ce.getMessage();
}

Hope that helps.

Regards,
Stefan W.
 
M

Manish Hatwalne

As I mentioned earlier the legacy code has unfortunately caught multiple
exceptions using the generic Exception; and I can't change it. It uses the
construct that I have given -

try{
doSomething();
// some code that can throw many exceptions
.........
}catch(Exception e){
throw new CustomException(e);
}

I have no control over this code (so can be treated as good as third party
library stuff), and I can only handle CustomException in my code while
calling this method. Is there sth I can do about it?

TIA,
- Manish



Stefan Waldmann said:
Manish said:
I have a legacy code (and I can't change this part) which uses construct
such as -

try{
doSomething();
.........
}catch(Exception e){
throw new CustomException(e);
}
[snip]

Why would you just catch a generic Exception? Normally when you use try
/ catch, you know which kind of Exception you are catching. So you could
create your CustomException there with the appropriate message.
For example:

try {
doDatabaseAccess();
}
catch(SQLException se) {
throw new CustomException("Database access failed.", se);
}

or

try {
String str = "123xy";
int i = Integer.parseInt(str);
catch(NumberFormatException nfe) {
throw new CustomException(str+" could not be parsed to an int.",nfe);
}

So you have your appropriate message already in your CustomException and
when necessary can get it by

catch(CustomException ce) {
String msg = ce.getMessage();
}

Hope that helps.

Regards,
Stefan W.
 
S

Stefan Waldmann

Manish said:
As I mentioned earlier the legacy code has unfortunately caught multiple
exceptions using the generic Exception; and I can't change it. It uses the
construct that I have given -

try{
doSomething();
// some code that can throw many exceptions
.........
}catch(Exception e){
throw new CustomException(e);
}
[snip]

Oops, sorry, must have overlooked this.

IMHO it's a bad design when all exceptions are just caught and
transformed into generic Exceptions. This leads to proplems just as the
one you are experiencing right now. I think whenever possible the
various Exception types given by the Java SDK should be used (there are
many of them for many various purposes), or if there isn't an
appropriate Java Exception, to create specific ones by yourself instead
just using one generic Exception...

But you wrote that you have no influence to the classes you use. Perhaps
one of the following constructs could be of use to you:

try {
// ...
}
catch(CustomException ce) {
String msg = ce.getCause().getMessage(); // gives you the exception
message of the cause exception if there is any
// or
msg = ce.getCause().getClass().getName(); // gives you the class
name of the cause exception
// or
msg = ce.getCause().toString(); // gives you the class name of the
cause exception and the exception message if there is any
}

HTH

Regards,
Stefan W.
 
C

Chris Uppal

Manish said:
try{
callThisMethod();
}catch(CustomException ce){
Throwable t = ce.getCause() ; //what should I use here??
if(t instanceof SomeException){
/// appropriate message
}
}

My problem is how do I get the exception wrapped by this CustomException?
I thought getCause() will return me that, but it returns CustomException.

Maybe the legacy code has wrapped the original exception in more than one layer
of CustomException. I.e. the original exception has been caught and
re-"thrown" several times. If so, you need a loop.

Or maybe it's just a bug in the legacy code that manages to set the cause
incorrectly (you did say it's legacy code, so presumably it pre-dates the
relatively recent getCause() stuff, and setting the cause must have been hacked
in afterwards somehow).

Does ce.getCause() return a reference to the same object ?

-- chris
 
G

Gabor Fenyvesi

Stefan Waldmann said:
Manish said:
I have a legacy code (and I can't change this part) which uses construct
such as -

try{
doSomething();
.........
}catch(Exception e){ throw new CustomException(e);
}
[snip]

Why would you just catch a generic Exception? Normally when you use try
/ catch, you know which kind of Exception you are catching. So you could
create your CustomException there with the appropriate message.
For example:

try {
doDatabaseAccess();
}
catch(SQLException se) {
throw new CustomException("Database access failed.", se);
}

or

try {
String str = "123xy";
int i = Integer.parseInt(str);
catch(NumberFormatException nfe) {
throw new CustomException(str+" could not be parsed to an int.",nfe);
}

So you have your appropriate message already in your CustomException and
when necessary can get it by

catch(CustomException ce) {
String msg = ce.getMessage();
}

Hope that helps.

Regards,
Stefan W.

Hm, he can't change the code.
It depends on the CustomException class. If you can change the source
of this, do that (ie hold the exception at the constructor, and write
a getSourceException() method).
If you can't, try to use e.printStackTrace(...), and analyze the
message.
 
G

Gabor Fenyvesi

Stefan Waldmann said:
Manish said:
I have a legacy code (and I can't change this part) which uses construct
such as -

try{
doSomething();
.........
}catch(Exception e){ throw new CustomException(e);
}
[snip]

Why would you just catch a generic Exception? Normally when you use try
/ catch, you know which kind of Exception you are catching. So you could
create your CustomException there with the appropriate message.
For example:

try {
doDatabaseAccess();
}
catch(SQLException se) {
throw new CustomException("Database access failed.", se);
}

or

try {
String str = "123xy";
int i = Integer.parseInt(str);
catch(NumberFormatException nfe) {
throw new CustomException(str+" could not be parsed to an int.",nfe);
}

So you have your appropriate message already in your CustomException and
when necessary can get it by

catch(CustomException ce) {
String msg = ce.getMessage();
}

Hope that helps.

Regards,
Stefan W.

Hm, he can't change the code.
It depends on the CustomException class. See the source, and find out,
or if you can change the source of this, do that (ie hold the
exception at the constructor, and write a getSourceException()
method).
If you can't, try to use e.printStackTrace(...), and analyze the
message.
 
M

Manish Hatwalne

Chris Uppal said:
CustomException.

Maybe the legacy code has wrapped the original exception in more than one layer
of CustomException. I.e. the original exception has been caught and
re-"thrown" several times. If so, you need a loop.

You are bang on target!!!
That was it, the exception was getting wrpeed several times in the
hierarchy, and hence was causing the problem.
Used recursion to get the cause.
Or maybe it's just a bug in the legacy code that manages to set the cause
incorrectly (you did say it's legacy code, so presumably it pre-dates the
relatively recent getCause() stuff, and setting the cause must have been hacked
in afterwards somehow).

This is also a problem in some places where the CustomException is just
being created with the message.
Can't help such situations though!

Thanks again for pointing me in the right direction!!!

- Manish
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top