Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Handling Getopt Exceptions

If the user gave any invalid options on the command-line, the parsing function throws a Zend_Console_Getopt_Exception. You should catch this exception in your application code. You can use the parse() method to force the object to parse the arguments. This is useful because you can invoke parse() in a try block. If it passes, you can be sure that the parsing won't throw an exception again. The exception thrown has a custom method getUsageMessage(), which returns as a string the formatted set of usage messages for all declared options.

Example 128. Catching Getopt Exceptions

try {
$opts = new Zend_Console_Getopt('abp:');
} catch (
Zend_Console_Getopt_Exception $e) {

Cases where parsing throws an exception include:

  • Option given is not recognized.

  • Option requires a parameter but none was given.

  • Option parameter is of the wrong type. E.g. a non-numeric string when an integer was required.

Zend Framework