I don't know if its possible in case of java exception like this one to not only have the java stack trace but also the xquery one to be able to easier spot the line of code generating the error.
It’s a bit difficult to catch these kinds of errors, because integer overflows and out-of-memory errors can occur all over the place… Let’s see, maybe we’ll find a nice way out.