PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How to print the stack trace of a caught exception using a one-liner?

 July 17, 2022     exception, matlab, stack-trace, try-catch, warnings     No comments   

Issue

I want to catch an exception, but print the same message it would print if not caught (stack trace). How to do that?

I tried

>> myfunctionwitherror
Error using myfunctionwitherror (line 3)
myerror

>> try myfunctionwitherror; catch disp(lasterror), end
Warning: This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB.
See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details. 
  MException with properties:

    identifier: ''
       message: 'myerror'
         cause: {0×1 cell}
         stack: [0×1 struct]

>> try myfunctionwitherror, catch e getReport(e), end
Warning: This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB.
See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details. 
Undefined function or variable 'e'.

How to accomplish this?


I am using 2016b. I don't know why this message appears.


Solution

You are using 2007a syntax for try catch.

it was:

try,
 statementA
catch,
 statementB
end

and now is

try
 statementA
catch e
 statementB
end

But as you are writing one liners, you forgot the ; so you are just confusing MATLAB about when the lines end, so its assuming you are doing

try 
   myfunctionwitherror 
catch 
  e 
  getReport(e)
end

Just put the semicolons where they should be when you are using ambiguous one liners. Or write multiple lines. ;)

 try; myfunctionwitherror; catch e; getReport(e); end;

If what you want is to display the error (without trowing an error) just

try; myfunctionwitherror; catch e; disp(e.message); end;


Answered By - Ander Biguri
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing