1 / 10

No runs, no drips, & no errors

No runs, no drips, & no errors. Error recovery. Oops!. Even the most meticulous among us make mistakes When life’s inevitable little boo boos do occur, graceful recovery is the ticket. Warnings and errors. By now, we are all woefully aware of MATLAB’s errors and warnings: >> a = b

angiew
Download Presentation

No runs, no drips, & no errors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. No runs, no drips, & no errors Error recovery

  2. Oops! Even the most meticulous among us make mistakes When life’s inevitable little boo boos do occur, graceful recovery is the ticket Curves & errors

  3. Warnings and errors By now, we are all woefully aware of MATLAB’s errors and warnings: >> a = b ??? Undefined function or variable 'b'. >> x = 1/0 Warning: divide by zero. x = Inf Curves & errors

  4. Warning! We can add our own warning messages to code that we write: function result = squareRoot(num) if (num < 0) warning (['taking squareroot of negative number ' … num2str(num)]); end result = sqrt(num); Calling this function with a negative number: >> n = squareRoot(-3) Warning: taking squareroot of negative number -3 n = 0 + 1.7321i Curves & errors

  5. Nicely formatted, useful warnings Format strings can be used with the warning function: function result = analyzeImage (image) [rows cols] = size(image); results = zeros(rows, cols, 'uint8'); for row = 2:rows for col = 2:cols result = <… some computation …> if (result < 0) warning('negative result for row %u, col %u: %4.2f', row, col, result); end results(row, col) = result; end end Curves & errors

  6. Error! In more dire circumstances we can issue our own error messagesthathalt execution of an M-file function result = analyze(x, y, z) if (nargin < 2) error ('At least two inputs are required'); elseif (nargin == 2) ... end >> n = analyze(3) ??? Error using ==> analyze At least two inputs are required Curves & errors

  7. Nicely formatted, useful error messages Format strings can be used with the error function: function result = analyzeImage (image) [rows cols] = size(image); results = zeros(rows, cols, 'uint8'); for row = 2:rows for col = 2:cols result = <… some computation …> if (result < 0) error('negative result for row %u, col %u: %4.2f', row, col, result); end results(row, col) = result; end end Curves & errors

  8. Graceful recovery We can prevent a MATLAB error from halting execution of an M-file by handling the error with a try-catch-endstatement try statements to execute catch code to handle errors generated by above statements end Curves & errors

  9. When bad things happen to good programs Some events are beyond our control: try data = textread('data.txt', '%s'); catch disp('file read was not successful'); data = rand(10,10); end result = analyze(data); Curves & errors

  10. Error analysis using lasterr or lasterror functionresult = matrixMultiply (A, B) try result = A * B; catch errormsg = lasterr; if (strfind(errormsg, 'Inner matrix dimensions')) disp('** Wrong dimensions for matrix multiplication') elseif (strfind(errormsg, … 'not defined for values of class')) disp('** Both arguments must be double matrices'); end result = A; end disp('done'); Curves & errors

More Related