MATLAB For Dummies, 2nd Edition
Book image
Explore Book Buy On Amazon

MATLAB supports both error and warning messages. You have a number of ways to create exceptions based on application conditions. The easiest way is to use the error() and warning() functions. The first creates an error condition, while the second creates a lesser, warning condition.

This example presents a basic method of issuing an error or warning due to user input. However, you can use the same technique whenever an error or warning condition arises and you can’t handle it locally.

function [ ] = ErrorAndWarning( )
%ERRORANDWARNING Create Error and Warning Messages
% This example shows how to create error and warning messages.
 NotDone = true;
 while NotDone
   Value = input('Type something: ', 's');
   switch Value
    case 'error'
     error('Input Error');
    case 'warning'
     warning('Input Warning');
    case 'done'
     NotDone = false;
     disp(['You typed: ', Value]);
  catch Exception
   disp('An exception occurred!');

The example begins by creating a loop. It then asks the user to type something. If that something happens to be error or warning, the appropriate error or warning message is issued. When the user types done, the application exits. Otherwise, the user sees a simple output message. The example looks simple, but it has a couple of interesting features. The following steps help you work with the example:

  1. Type ErrorAndWarning() and press Enter in the Command window.

    The application asks you to type something.

  2. Type Hello World! and press Enter.

    You see the following output:

    You typed: Hello World!

    The application asks the user to type something else.

  3. Type warning and press Enter.

    You see the following output:

    Warning: Input Warning
    > In ErrorAndWarning at 16

    Notice that the message doesn’t say anything about an exception. A warning is simply an indicator that something could be wrong, not that something is wrong. As a result, you see the message, but the application doesn’t actually generate an exception. The application asks the user to type something else.

  4. Type error and press Enter.

    You see the following output:

    An exception occurred!
    Error using ErrorAndWarning (line 14)
    Input Error

    This time, an exception is generated. If the exception handler weren’t in place, the application would end at this point. However, because an exception handler is in place, the application can ask the user to type something else. Adding exception handlers makes recovering from exceptions possible, as happens in this case. Of course, your exception handler must actually fix the problem that caused the exception.

  5. Type done and press Enter.

    The application ends.

The example application uses the simple form of the error() and warning() functions. Both the error() and warning() functions can accept an identifier as the first argument, followed by the message as the second. You can also add the cause and stack trace elements as arguments. The point is, all you really need in most cases is a simple message.

About This Article

This article is from the book:

About the book authors:

Jim Sizemore is professor of physics and engineering at Tyler Junior College. For over 25 years, Sizemore has worked in the semiconductor and software industries as a process engineer, device physicist, and software developer. John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++.

This article can be found in the category: