beanshell / beanshell

Beanshell scripting language
Apache License 2.0
815 stars 183 forks source link

Try-catch loses traceback information #726

Open opeongo opened 1 year ago

opeongo commented 1 year ago

In the following script the error occurs at line 5.

try {
    /*
     * Trigger an error
     */
    o = (Number) Double.valueOf(1);
} catch (Exception ex) {
    ex.printStackTrace();
}

However the traceback information indicates that the error is at line 1 (the start of the try block):

bsh.EvalError: Sourced file: testCast.bsh unknown error: class java.lang.Double cannot be cast to class bsh.Primitive (java.lang.Double is in module java.base of loader 'bootstrap'; bsh.Primitive is in unnamed module of loader ca.spatial.util._bF @7a9273a8) : at Line: 1 : in file: testCast.bsh : try {

Caused by: Sourced file: testCast.bsh unknown error: class java.lang.Double cannot be cast to class bsh.Primitive (java.lang.Double is in module java.base of loader 'bootstrap'; bsh.Primitive is in unnamed module of loader ca.spatial.util._bF @7a9273a8) : at Line: 1 : in file: testCast.bsh : try {

Caused by: class java.lang.Double cannot be cast to class bsh.Primitive (java.lang.Double is in module java.base of loader 'bootstrap'; bsh.Primitive is in unnamed module of loader ca.spatial.util._bF @7a9273a8)

This loss of traceback information makes it difficult to locate the source of problems. It would be better if the full traceback of all previous chained exceptions was also displayed.

nickl- commented 1 year ago

I am confused though, this doesn't look like the output of the printStackTrace.

We have the correct error:

Caused by: class java.lang.Double cannot be cast to class bsh.Primitive (java.lang.Double is in module java.base of loader 'bootstrap'; bsh.Primitive is in unnamed module of loader ca.spatial.util._bF @7a9273a8)

And I am willing to bet it has the correct context too, somewhere we need to unwrap to get to this error. Hence my confusion whether this comes from Exception ex or not. We need to identify where the output comes from first.