MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 586 and 587: 7 Working with Scientific Data Form
- Page 588 and 589: 7 Working with Scientific Data Form
- Page 590 and 591: 7 Working with Scientific Data Form
- Page 592 and 593: 7 Working with Scientific Data Form
- Page 594 and 595: 7 Working with Scientific Data Form
- Page 596 and 597: 7 Working with Scientific Data Form
- Page 598 and 599: 7 Working with Scientific Data Form
- Page 600 and 601: 7 Working with Scientific Data Form
- Page 602 and 603: 7 Working with Scientific Data Form
- Page 604 and 605: 7 Working with Scientific Data Form
- Page 606 and 607: 7 Working with Scientific Data Form
- Page 608 and 609: 7 Working with Scientific Data Form
- Page 610 and 611: 7 Working with Scientific Data Form
- Page 612 and 613: 7 Working with Scientific Data Form
- Page 614 and 615: 7 Working with Scientific Data Form
- Page 616 and 617: 7 Working with Scientific Data Form
- Page 618 and 619: 7 Working with Scientific Data Form
- Page 620 and 621: 7 Working with Scientific Data Form
- Page 622 and 623: 7 Working with Scientific Data Form
- Page 624 and 625: 7 Working with Scientific Data Form
- Page 626 and 627: 7 Working with Scientific Data Form
- Page 628 and 629: 7 Working with Scientific Data Form
- Page 630 and 631: 7 Working with Scientific Data Form
- Page 632 and 633: 7 Working with Scientific Data Form
- Page 634 and 635: 7 Working with Scientific Data Form
- Page 638 and 639: 8 Error Handling Handling and Recov
- Page 640 and 641: 8 Error Handling X = A * B catch di
- Page 642 and 643: 8 Error Handling matrixMultiply(A,
- Page 644 and 645: 8 Error Handling Message Identifier
- Page 646 and 647: 8 Error Handling error('msg_id', 'e
- Page 648 and 649: 8 Error Handling Warnings Like erro
- Page 650 and 651: 8 Error Handling Warning Control Th
- Page 652 and 653: 8 Error Handling warnings by issuin
- Page 654 and 655: 8 Error Handling You must type the
- Page 656 and 657: 8 Error Handling s(2) ans = identif
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 662 and 663: 9 Classes and Objects Classes and O
- Page 664 and 665: 9 Classes and Objects The diagram s
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
- Page 678 and 679: 9 Classes and Objects A(1,2).name(3
- Page 680 and 681: 9 Classes and Objects Object Indexi
- Page 682 and 683: 9 Classes and Objects Converter Met
- Page 684 and 685: 9 Classes and Objects Operation M-F
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