public
Last active

Exceptions with deep causes. JRuby only shows the latest level, i.e. 'Exception on level 8' without any insight into the deeper causes.

  • Download Gist
NestedExceptionTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class NestedExceptionTest {
 
public void throwNestedExceptions(int n) {
if (n > 0) {
try {
throwNestedExceptions(n-1);
} catch (Exception e) {
throw new RuntimeException("Exception on level " + n, e);
}
} else {
throw new RuntimeException("Root cause!");
}
}
 
public static void main(String[] ignored) {
new NestedExceptionTest().throwNestedExceptions(8);
}
 
}
nested_exceptions.rb
Ruby
1 2 3 4
require 'java'
 
$CLASSPATH << File.expand_path('..', __FILE__)
Java::NestedExceptionTest.new.throw_nested_exceptions 8

I would expect the JRuby stack trace to contain some information about the root cause but instead it only shows

NativeException: java.lang.RuntimeException: Exception on level 8
  (root) at nested_exceptions.rb:5

without a mention of the root cause.
So, how should I get that information saved, say, when logging? Should I manually inspect NativeException and see what it contains?

I don't think Ruby can throw nested exceptions; I believe JRuby mimics this behavior.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.