Go to: Synopsis. Return value. Related. MEL examples.
catch
(expression)
catch is undoable, NOT queryable, and NOT editable.
This keyword takes a MEL expression as an argument. It will return 1 if the expression in parenthesis produces an error at runtime and 0 otherwise. This expression is designed to allow users to check for runtime failure and recover, if desired. The use of catch stops a runtime error from being propagated back up through the execution path of the script. Normally, a runtime error results in the termination of the execution of a script or procedure. The catch command will not catch warnings, only errors. The MEL code inside the parenthesis of the catch statement should be a single expression such as a call to a MEL command or procedure. See examples below. Note: catch is not a command. It is a keyword in the Mel language. Its usage in Mel scripts looks more like that of procedure calls than of command invocations.| int | 0 or 1 |
// Call to MEL command
//
if ( catch( `createNode -name myTransform transform` ) ) {
print "Could not create sphere\n";
} else {
// continue on as usual...
}
// Use of MEL C style procedure call syntax
//
if ( catch( advanceToNextDrivenKey( "","" ) ) ) {
print "Could not advance\n";
} else {
// continue on as usual...
}
// Set $divisor to 0 just to trigger an exception
//
int $divsor = 0;
int $factor;
if ( catch ($factor == 42/$divsor) ) {
print "Attempt to divide by zero caught\n";
} else {
// continue on as usual...
}
// This example shows how catch can be used to handle failure of commands
// or procedures. This will catch errors and calls to procedures that do
// not exist.
catch ( `underConstruction` );
// Without the catch, if the execution of underConstruction failed at
// runtime, the whole script containing the call would fail. Using catch
// enables this script to continue execution even if something fails.