How to work around broken third party Exception class

  • Thread starter Brian J. Sayatovic
  • Start date
B

Brian J. Sayatovic

I'm using a third-party library whose API throws various Exceptions
that all extend one of their base exception classes. However, in some
cases, their library is throwing instances of their Exceptions which
throw a NullPointerException when calling .getMessage() (remember,
getMessage() is also called implicitly by toString() and
printStackTrace())...

try {
// ...
}
catch(ThirdPartyException tpe) {
// *** Throws a NullPointerException!
System.out.println(tpe.getMessage());
}

I've found out this is because their Exceptions use a "details" object
to carry the message, so I'm assuming their getMessage() method
accesses this details object to get the message to return. However,
in some cases, they do NOT initialize this details object for the
Exception (I actually read their docs where it says they will
initialize it to a default instance, but in practice they don't --
I've tested). However, they do expose a method to check the details,
getDetails().

So, how can I print out the stack trace of these exceptions so I can
at least find their source without the accompanying description? I
already tried using reflection to check the private field they store
the information in, make it accessible and then set the instance if it
is null; however, I ran into security problems and can't realisticly
change the policy for production. I thought about making a wrapping
delegate, but I couldn't quite figure out how it would work.

This is a pseudo-API of their Exception base class, if it helps:

public class ThirdPartyException extends Exception {

// Leaves the details member set to null
public ThirdPartyException();

// Sets the third-party details appropriately
public ThirdPartyException(ThirdPartyDetails details);

// Returns the message from the third-party details
public String getMessage();

// Returns the member
public ThirdPartyDetails getDetails();

}

public class ThirdPartyDetails() {
// Returns the actual message descripting the exception
public String getMessage();
}

Regards,
Brian.
 
T

Thomas Weidenfeller

So, how can I print out the stack trace of these exceptions so I can
at least find their source without the accompanying description?

Without knowing much details, here is what I would try:

1) Call them and ask for a bug fix.

2) Call them, yell at them and ask for a bug fix

3) Call them, yell at them and ask for my money back

If this doesn't help, I would consider decompiling the library, fix the
constructor, and re-package the library. You should, however, check the
license and laws first.

YMMV

/Thomas
 
R

rkm

Brian said:
So, how can I print out the stack trace of these exceptions so I can
at least find their source without the accompanying description?

Create your own ThirdPartyException class and replace theirs
with yours?
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top