API Guide > Maya API introduction >

# Error checking

In the examples presented so far you have not been prompted to do much error checking. This is usually fine for examples, but when producing a production plug-in you really want to check for errors.

Most methods take an optional final argument, which is a pointer to an MStatus variable into which the status return value is put.

If you replace the argument parsing code with the following fragment in the helix example, the example will be checking for, and handling, most possible errors.

    // Parse the arguments.
    for ( i = 0; i < args.length(); i++ )
        if ( MString( "-p" ) == args.asString( i, &stat )
            && MS::kSuccess == stat )
        {
            double tmp = args.asDouble( ++i, &stat );
            // argument can be retrieved as a double
            if ( MS::kSuccess == stat )
                pitch = tmp;
        }
        else if ( MString( "-r" ) == args.asString( i, &stat )
            && MS::kSuccess == stat )
        {
            double tmp = args.asDouble( ++i, &stat );
            // argument can be retrieved as a double
            if ( MS::kSuccess == stat )
                radius = tmp;
        }

The addition of &stat to the asString() and asDouble() methods allows you to check if the casting operation succeeds.

For example, args.asString(i, &stat) could return MS::kFailure if the index is greater than the number of arguments,

or

args.asDouble(++i, &stat) could fail if the argument could not be converted to a double.