MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

8 Error Handling Checking for Errors with try-catch No matter how carefully you plan and test the programs you write, they may not always run as smoothly as expected when run under different conditions. It is always a good idea to include error checking in programs to ensure reliable operation under all conditions. When you have statements in your code that could possibly generate unwanted results, put those statements into a try-catch block that will catch any errors and handle them appropriately. The example below shows a try-catch block within a sample function that multiplies two matrices: function matrixMultiply(A, B) try X = A * B catch disp '** Error multiplying A * B' end A try-catch block is divided into two sections. The first begins with try and the second with catch. Terminate the block with end. • All statements in the try segment are executed normally, just as if they were in the regular code flow. But if any of these operations results in an error, MATLAB skips the remaining statements in the try and jumps to the catch segment of the block. • The catch segment handles the error. In this example, it displays a general error message. If there are different types of errors that can occur, you will want to identify which error has been caught and respond to that specific error. You can also try to recover from an error in the catch section. 8-2

Checking for Errors with try-catch When you execute the above example with inputs that are incompatible for matrix multiplication (e.g., the column dimension of A is not equal to the row dimension of B),MATLABcatchestheerroranddisplaysthemessage generated in the catch section of the try-catch block. A = [1 2 3; 6 7 2; 0 1 5]; B = [9 5 6; 0 4 9]; matrixMultiply(A, B) ** Error multiplying A * B Note Faulty error statements executed within a try block are not caught, but instead cause MATLAB to abort the M-file. Nested try-catch Blocks You can also nest try-catch blocks, as shown here. You can use this to attempt to recover from an error caught in the first try section. try statement1 catch try statement2 catch disp 'Operation failed' end end % Try to execute statement1 % Attempt to recover from error % Handle the error 8-3

8 Error Handling<br />

Checking for Errors with try-catch<br />

No matter how carefully you plan and test the programs you write, they may<br />

not always run as smoothly as expected when run under different conditions.<br />

It is always a good idea to include error checking in programs to ensure<br />

reliable operation under all conditions.<br />

When you have statements in your code that could possibly generate<br />

unwanted results, put those statements into a try-catch block that will<br />

catch any errors and handle them appropriately. The example below shows a<br />

try-catch block within a sample function that multiplies two matrices:<br />

function matrixMultiply(A, B)<br />

try<br />

X = A * B<br />

catch<br />

disp '** Error multiplying A * B'<br />

end<br />

A try-catch block is divided into two sections. The first begins with try and<br />

the second with catch. Terminate the block with end.<br />

• All statements in the try segment are executed normally, just as if they<br />

were in the regular code flow. But if any of these operations results in<br />

an error, <strong>MATLAB</strong> skips the remaining statements in the try and jumps<br />

to the catch segment of the block.<br />

• The catch segment handles the error. In this example, it displays a general<br />

error message. If there are different types of errors that can occur, you will<br />

want to identify which error has been caught and respond to that specific<br />

error. You can also try to recover from an error in the catch section.<br />

8-2

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

Saved successfully!

Ooh no, something went wrong!