Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling stack traces with unknown exception classes

I'm implementing a session bean that throws ApplicationExceptions. These exceptions have chained stack traces that may contain exceptions whose classes aren't available on the client. Something like:

@Override
public void doSomethingSpecial(MyObject o) throws MyException {
    try {
        legacySystem.handle(o);
    } catch (LegacyException e) {
        logger.warn(e.getMessage(), e);
        throw new MyException(e);
    }
}

Here it's possible that the client gets an exception it doesn't have the class for. This can result in:

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
    at sun.proxy.$Proxy0.doSomethingSpecial(Unknown Source)
    at com.myapp.client.Client.main(Client.java:56)
Caused by: java.lang.ClassNotFoundException: MyLegacyException

I don't want the client to know all the possible exceptions that can be thrown on the server side, but having a stack trace is never bad.

How do you handle these problems? Is it a passable solution to implement an Interceptor that decouples the stack trace when the exception is sent back to the client? But then the Interceptor should handle only calls via the RemoteInterface, because internally I'm interested in the whole stack trace.

like image 994
boskop Avatar asked Sep 20 '13 07:09

boskop


People also ask

How can I print stack trace without exception?

Just use new Throwable(). printStackTrace() method and it will print complete stack trace from where a method is called, into the console. Main difference between using dumpStack() and printStackTrace() is first entry in Stack, In case of dumpStack() first entry is always java.

What is a stack trace and how does it relate to an exception?

Simply put, a stack trace is a representation of a call stack at a certain point in time, with each element representing a method invocation. The stack trace contains all invocations from the start of a thread until the point it's generated. This is usually a position at which an exception takes place.

Should stack traces be logged?

Logging the stack traces of runtime exceptions assists developers in diagnosing runtime failures. However, unnecessary logging of exception stack traces can have many negative impacts such as polluting log files.


1 Answers

It depends on your client type. If a client is another team which is developing another component or subsytem, I'm agree with you about:

Having a stack trace is never bad

But if they are customers who have no idea about your application internals, so there is no reason for them to know your exception classes or even see your stack traces. It would be nice to have a protocol which force you to catch all exceptions and wrap them in a high level exception class with a error_code property. This way, you can have a specific error code for each catch statement in your application and you will give your clients a list of these codes.

Anyway, from technical view, if your clients doesn't have access to your internal Exception classes, so they can't have access to your stack trace without referred ClassNotFoundException. If you really want them to see the stack trace, one solution could be to have an Aspect which sits just on the most upper layer of your API (which is going to be called by clients) and catches all the exceptions, writes their stack traces in a String and sends this as a property of the final exception which is going to be caught by caller. This way, the caller can access the stack trace as a formatted String property of the exception.

Edit:

You can even configure your build script, so that this Aspect never be a part of your release versions. So you can give this stack trace messages just in your debug version.

like image 84
zaerymoghaddam Avatar answered Oct 01 '22 15:10

zaerymoghaddam