tags:

views:

243

answers:

2

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?

A: 

Does using the FORMAT command improve the display at all? This helps display variables in a more compact form:

format compact

It may help display error messages in a more compact form too.

gnovice
Afraid not. The error message is displayed the same under format compact or loose.
Andrew Janke
+1  A: 

When the MATLAB Runtime intercepts the java exception, it uses the system line endings when it wraps it in a MException. To get around it, you can throw the exception on a different thread, or send the stack trace directly to stderr, such as with printStackTrace():

public class Bummer {
 public static void a() { b(); }
 public static void b() { c(); }
 public static void c() { d(); }
 public static void d() { new RuntimeException("bummer").printStackTrace(); }
}

Of course, it is a very bad thing to get a java exception in MATLAB. If you're using exceptions for things that are not really exceptional, then you may want to consider wrapping with a MException your users will find useful.

If you're using the exceptions to debug your java code, I find the stack trace analyzing functions of most java IDE's are able to deal with the extra line breaks nicely.

Mike Katz