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.
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.