0

When a stored Java class hits and unhandled exception, what is the best way to get the full stack trace back to PL/SQL for proper logging?

During preliminary tests of my class, I hit a NullPointer, which Oracle displayed as an unhandled Java Exception. The only info I had was that it was a NullPointer - I get no hint as to where it is in the code.

Obviously I should be able to avoid most exceptions with rigorous validation of the client-provided data and unit tests, but in the event something happens?

I was thinking of catching exceptions, storing their stacktrace in an array and returning that array to PLSQL... but I'd rather avoid the need to manage all these additional return values.

There is most definitely already something on that, unfortunately my Google-fu has failed me...

tdtm
  • 334
  • 2
  • 13

1 Answers1

2

You can get the complete stacktrace and convert them into a String and pass it to PL/SQL for logging. I got the simple solution for converting Stacktrace to String from another SO question here

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
String stackTrace = sw.toString(); // Send it to PL/SQL
Community
  • 1
  • 1
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • I went with this. I created them as static variables and made a wrapper function `log(Exception)` and `log(String)` (with a `pw.append` to add my own messages) for development/debug that either sends it to System.out or this depending on if I'm running it on local JVM or on the database JVM. I later had to capture the debugging messages of an external API done over System.out, but I had no access to the code to do what you suggested, so I had to use something similar to http://stackoverflow.com/questions/8708342/redirect-console-output-to-string-in-java . – tdtm Jul 03 '14 at 15:21
  • Glad, that you were able to do it. Thanks – Keerthivasan Jul 04 '14 at 04:03