Hello everyone,
I'm trying to learn Stata, having a background in general programming. I have a small question about the best practice for catching exceptions in Stata. Suppose I want to do get a varlist with all the variables starting with p in my dataset, if they are any. I would do:
Now suppose there is no such variable, I would get the following error:
The solution I see the most frequently on Statalist and other forums to this type of problem is the following
To me, there is a problem here; "if _rc" catches any exceptions. This is discouraged in other programming languages because if some other unexpected error occurs in you code, you don't see it until something breaks later in the code. We know we expect a r(111) exception, so the way I would "intuitively" write it would be:
How come Stata programmers use the former? Am I missing something? Thank you very much!
I'm trying to learn Stata, having a background in general programming. I have a small question about the best practice for catching exceptions in Stata. Suppose I want to do get a varlist with all the variables starting with p in my dataset, if they are any. I would do:
Code:
ds p*
Code:
ds p* variable p* not found r(111);
Code:
capture ds p* if _rc { *do something if no p* } else { *do something with the p* variables }
Code:
capture ds p* if _rc == 111 { *do something if no p* } else if _rc == 0 { *do something with the p* variables } else { * return the error code and exits the do-file execution error _rc exit }
Comment