Bidirectional Java stacktrace in Matlab errors

In Matlab, when a Java exception is thrown by a Java method called from M code, it is converted to a Matlab error or MException, and the Java stack stack is included in the MException message. In Windows, the stop trace is displayed at double intervals. 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"); }
}

      

What makes it.

>> 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 stop.

I believe this is because part of the stacktrace of the error message is being built with DOS mode CRLF (\ r \ n) line endings. This makes sense on a Windows machine. But Matlab command window is kind of Unix mode and converts \ r \ n to two linear pipes.

Our current solution is to use a try / catch guard code like this in 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, contains additional data in the message, and then calls RETHROW.

Adding a workaround is a bit tedious and prone to lack. And if you do "dbstop if all error", it will not capture the error display at that point. It's a little annoyance in the end, but when you spend all day debugging Matlab code it adds up. And I'm curious about the Java / MException integration mechanism.

Is there a way to configure Matlab to plot the stacktrace portion of the error message text using \ n line separators so that it displays the single-split in the command window?

+2


source to share


3 answers


When MATLAB Runtime catches a java exception, it uses system line endings when it terminates in MException. To get around this, you can throw the exception on a different thread, or send the stack trace directly to stderr, for example using 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's too bad to get Java exception in MATLAB. If you are using exceptions for things that are not really exceptional, then you may want to consider porting with MException, which your users will find useful.

If you are using exceptions to debug your java code, I have found that the stack trace analysis functions from most java IDEs are capable of dealing with unnecessary line breaks.

+1


source


Try to install:



java.lang.System.setProperty('line.separator', sprintf('\n'));

      

+1


source


Does the FORMAT command use display enhancements at all? This helps to display variables in a more compact form:

format compact

      

This can help display error messages in a more compact form.

0


source







All Articles