23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8 Error Handling<br />

matrixMultiply(A, C)<br />

** Both arguments must be double matrices<br />

Regenerating an Error<br />

Use the rethrow function to regenerate an error that has previously been<br />

thrown.Youmightwanttodothisfromthecatch part of a try-catch block,<br />

for example, after performing some required cleanup tasks following an error.<br />

This is the general method used to rethrow an error in a try-catch block:<br />

try<br />

- do_something -<br />

catch<br />

- do_cleanup -<br />

rethrow(lasterror)<br />

end<br />

rethrow generates an error based on the <strong>MATLAB</strong> structure you provide<br />

as an input argument. The input structure must have at least one of the<br />

message, identifier, andstack fields.<br />

Field Name<br />

message<br />

identifier<br />

stack<br />

Description<br />

Character array containing the text of the error<br />

message.<br />

Character array containing the message identifier of<br />

the error message. If the last error issued by <strong>MATLAB</strong><br />

had no message identifier, then the identifier field<br />

is an empty character array.<br />

Structure providing information on the location of<br />

the error. The structure has fields file, name, and<br />

line, and is the same as the structure returned by<br />

the dbstack function. If lasterror returns no stack<br />

information, stack is a 0-by-1 structure having the<br />

same three fields.<br />

If you simply want to regenerate the last error that occurred, the lasterror<br />

function returns a structure that can then be passed directly to rethrow.<br />

8-8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!