补充一句。capture是可以用来检查命令的return code的。所以本质上和try ... catch的功能是差不多的。不过大部分问题其实并不需要通过检查return code解决。很多时候,像这里的这个例子,简单的加一句cap log close,就能解决。这里发一下相关的帮助文件,方便后来人。
- Syntax
- capture [:] command
- capture {
- stata_commands
- }
- Description
- capture executes command, suppressing all its output (including error messages, if any) and issues a return code
- of zero. The actual return code generated by command is stored in the built-in scalar _rc.
- capture can be combined with {} to produce capture blocks, which suppress output for the block of commands. See
- the technical note in [P] capture for more information.
- Remarks
- capture is useful in do-files and programs because their execution terminates when a command issues a nonzero
- return code. Preceding sensitive commands with the word capture allows the do-file or program to continue despite
- errors. Also do-files and programs can be made to respond appropriately to any situation by conditioning their
- remaining actions on the content of the scalar _rc.
- capture can be combined with noisily to display the output and any error messages regardless of the return code.
- For example,
- . capture noisily regress y x
- will either display an error message and store the return code in _rc or display the output and store a return
- code of zero in _rc.
复制代码