getReport

Class: MException

Get error message for exception

Syntax

msgString = getReport(exception)
msgString = getReport(exception,type)
msgString = getReport(exception,type,'hyperlinks',hlink)

Description

msgString = getReport(exception) gets the error message for an exception and returns it as a formatted string, msgString. The message string is the value of the message property of the MException object, exception. It is the same string that MATLAB® displays when it throws the exception.

msgString = getReport(exception,type) returns the error message using the indicated level of detail, specified by type.

msgString = getReport(exception,type,'hyperlinks',hlink) uses the value of hlink to determine whether to include active hyperlinks to the failing lines of code within the error message.

Input Arguments

expand all

exception — Exception object that provides error messageMException object

Exception object that provides the error message, specified as a scalar MException object.

type — Detail indicator of message string'extended' (default) | 'basic'

Detail indicator of the message string returned, specified as 'extended' or 'basic'.

type ValuemsgString Detail Level
'extended' (default)msgString includes the line number, error message, cause, and stack summary. To display the proper stack, MATLAB first must throw an exception.
'basic'msgString includes the error message.

Hyperlink indicator of the message string that includes active hyperlinks to the failing lines of code, specified as 'on', 'off', or 'default'.

hlink ValueAction
'on'Display hyperlinks to failing lines of code.
'off'Do not display hyperlinks to failing lines of code.
'default'Use the default for the Command Window to determine whether or not to use hyperlinks in the error message.

Examples

expand all

Get Error Message from Exception

Cause MATLAB to throw an exception.

plus
Error using +
Not enough input arguments.

Get the error message from the exception.

exception = MException.last;
msgString = getReport(exception)
msgString =

Error using +
Not enough input arguments.

Specify Detail Level in Error Message

In a file in your current working folder, create the following function in testFunc.m.

function a = testFunc
try
    a = notaFunction(5,6);
catch a

end

Since the function, notaFunction, does not exist, testFunc returns an mException object.

At the command prompt, call testFunc and get the error message.

m = testFunc;
msgString = getReport(m)
msgString =

Undefined function 'notaFunction' for input arguments of type 'double'.

Error in testFunc (line 3)
    a = notaFunction(5,6);

Specify that the error message only contains the error message and not the stack information.

msgString = getReport(m,'basic')
msgString =

Undefined function 'notaFunction' for input arguments of type 'double'.

Turn Off Hyperlinks in Error Message

Cause MATLAB to throw an exception.

try 
    surf
catch exception
end

Get the error message from the exception.

msgString = getReport(exception)
msgString =

Error using surf (line 49)
Not enough input arguments.

Get the error message without active hyperlinks to surf.m.

msgString = getReport(exception,'extended','hyperlinks','off')
msgString =

Error using surf (line 49)
Not enough input arguments.
Was this topic helpful?