2

We have a third party java library; Inside which the exceptions are not properly handled.

Like this below:

public class UserClass {

    public static void main(String[] args) {

        try {
          // Do something and a exception gets thrown
        } catch (Exception e) {
          StupidLogger.debug("Exception Occured"); // Here as the exception is not logged we don't know what has actually happened.
        }

    }
}

As I do not have access to the code of the UserClass, I cannot change the code inside.

I have access to the StupidLogger class code. So, is it possible to get access to the Exception object either by stack trace or by using Runtime class or by any other way inside the debug() method of StupidLogger class and log it properly.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Ravi
  • 329
  • 1
  • 5
  • 17
  • 5
    If it's caught, it's gone. – Unihedron Aug 13 '14 at 06:24
  • 1
    Why do you want access to the `Exception` at all? Just logging a message can be the right thing to do. If the third-party code is not doing appropriate clean-up in the face of an exception, that would be a bug that you should raise with its developers. Or do you want to print a stack trace? That is not always appropriate; see http://stackoverflow.com/questions/7361201/when-to-log-a-stacktrace-for-a-caught-exception – Raedwald Aug 13 '14 at 06:51
  • *"If it's caught, it's gone."*. Not necessarily. The reference to the `Exception` object may be in a local variable in the stackframe of `main` ... depending on whether or not the JIT compiler has optimized away the variable. – Stephen C Aug 13 '14 at 07:06
  • @StephenC Still, once the `catch` block finishes executing, the declaration (and hence scope) is finished. You could of kept the reference or passed it to another variable which lasts in a _different_ scope, but it's still gone once it's caught. – Unihedron Aug 13 '14 at 07:08
  • 1
    @Unihedron - but `e` is still in scope (within `main`) while you are calling `debug`, and that's when the OP wants to access it. The fact that nothing in the handler in `main` (as written) accesses `e` doesn't make it out of scope. – Stephen C Aug 13 '14 at 07:10
  • So, I do not have a solution. thanks all for trying to help me out here – Ravi Aug 14 '14 at 03:13

2 Answers2

1

So, is it possible to get access to the Exception object either by stack trace

No.

or by using Runtime class

No

or by any other way inside the debug() method of StupidLogger class

No.

There is no JVM bytecode, and no reflection method that allows a method to look into the stackframe of an method call other than the current one.

I think that the only possibility would be to dive into native code, and start grovelling around with the address of the parent stack frame. You might even need to resort to calling into native assembly code from (say) a JNI or JNA method, in order to do it!


Don't go there. The correct solution is to change the API for StupidLogger and pass the exception references explicitly. And if you can't, then complain to the vendor ... or whoever provided the software that you can't debug because of its poor design.


Actually, if the local variable e in main is not used, then the JIT compiler may optimize it away. That would mean that grovelling around trying to find it from native code could be futile.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

I guess there are other methods in StupidLogger then debug, and one of them (probably error) accept Throwable. If not, you can make your own Logger class extending StupidLogger, and make method debug(String msg, Throwable T), do whatever you want with T, and invoke debug(msg).

Max Adamek
  • 73
  • 4
  • "_As i do not have access to the code of the UserClass, I cannot change the code inside._" This won't work for OP. – Unihedron Aug 13 '14 at 06:34