楼主: tiesuoqiao
2316 3

[编程问题求助] Stata没有try ... catch结构? [推广有奖]

  • 1关注
  • 8粉丝

已卖:78份资源

副教授

85%

还不是VIP/贵宾

-

威望
0
论坛币
487 个
通用积分
309.8531
学术水平
15 点
热心指数
12 点
信用等级
9 点
经验
54652 点
帖子
324
精华
0
在线时间
1589 小时
注册时间
2009-5-29
最后登录
2025-11-24

楼主
tiesuoqiao 发表于 2015-12-16 02:52:05 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我现在代码是

log using ...   //打开log

其他代码

log close  //关闭

如果其他代码出了问题,log close 就不执行了,下次需要手动关闭

怎么就没有这类:
log using ...   //打开log
try {
其他代码

log close  //关闭
} catch
{
og close  //关闭
}



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Stata Catch ATCH tata Try

沙发
夏目贵志 发表于 2015-12-16 03:44:09
在log using的前一行加上cap log close就可以解决问题了。
已有 1 人评分经验 论坛币 学术水平 热心指数 收起 理由
hustchen2012 + 10 + 10 + 1 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 10  学术水平 + 1  热心指数 + 1   查看全部评分

藤椅
tiesuoqiao 发表于 2015-12-16 06:46:41
夏目贵志 发表于 2015-12-16 03:44
在log using的前一行加上cap log close就可以解决问题了。
谢谢!

板凳
夏目贵志 发表于 2015-12-16 10:13:59
补充一句。capture是可以用来检查命令的return code的。所以本质上和try ... catch的功能是差不多的。不过大部分问题其实并不需要通过检查return code解决。很多时候,像这里的这个例子,简单的加一句cap log close,就能解决。这里发一下相关的帮助文件,方便后来人。
  1. Syntax

  2.         capture [:] command


  3.         capture {
  4.                 stata_commands
  5.         }


  6. Description

  7.     capture executes command, suppressing all its output (including error messages, if any) and issues a return code
  8.     of zero.  The actual return code generated by command is stored in the built-in scalar _rc.

  9.     capture can be combined with {} to produce capture blocks, which suppress output for the block of commands.  See
  10.     the technical note in [P] capture for more information.


  11. Remarks

  12.     capture is useful in do-files and programs because their execution terminates when a command issues a nonzero
  13.     return code.  Preceding sensitive commands with the word capture allows the do-file or program to continue despite
  14.     errors.  Also do-files and programs can be made to respond appropriately to any situation by conditioning their
  15.     remaining actions on the content of the scalar _rc.

  16.     capture can be combined with noisily to display the output and any error messages regardless of the return code.
  17.     For example,

  18.         . capture noisily regress y x

  19.     will either display an error message and store the return code in _rc or display the output and store a return
  20.     code of zero in _rc.
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-5 18:58