In Matlab, when a Java exception is thrown by a Java method invoked from M-code, it is converted to a Matlab error or MException, and the Java stacktrace is included in the MException's message. On Windows, the stacktrace is displayed double spaced. Here's an example.
package test;
public class Bummer {
public static void a() { b(); }
public static void b() { c(); }
public static void c() { d(); }
public static void d() { throw new RuntimeException("bummer"); }
}
Which produces this.
>> test.Bummer.a()
??? Java exception occurred:
java.lang.RuntimeException: bummer
at test.Bummer.d(Bummer.java:8)
at test.Bummer.c(Bummer.java:7)
at test.Bummer.b(Bummer.java:6)
at test.Bummer.a(Bummer.java:5)
>> disp(find(lasterr == sprintf('\r')))
61 95 129 163 197
This hurts readability, especially when you have a twenty call deep stack.
I believe this is because the stacktrace part of the error message is being constructed with DOS mode CRLF (\r\n) line endings. Which makes sense on a Windows machine. But the Matlab command window is kind of Unix mode, and converts \r\n to two line feeds.
Our current workaround is to use try/catch guard code like this around most Java method calls.
try
somejavaobject.SomeMethod();
catch err
rethrowmsg(err, 'Some additional details');
end
Rethrowmsg() is a function we wrote that munges err to convert \r\n to \n, incorporates additional details in the message, and then calls RETHROW.
Adding the workaround is a bit tedious and prone to be left out. And if you're doing "dbstop if all error", it won't fix the display of the error at that point. In the end, this is a minor annoyance, but when you spend all day debugging Matlab code, it adds up. And I'm curious about the Java exception/MException integration mechanism.
Is there a way to configure Matlab to construct the stacktrace part of the error message text with \n line separators so it displays single-spaced in the command window?