Really you'd want to set a variable equal to the $TLEVEL at the start of your try, then in the catch calculate the difference between the starting $TLEVEL and the current $TLEVEL, then do that many TROLLBACK 1s.
Combining what you said about error handling and transactions and also returning a status, a very basic outline for a lot of methods could be something like:
try{
TSTART
//Do stuff here
TCOMMIT
return $$$OK
}
catch ex{
if $TLEVEL > 0{
TROLLBACK
}
return ex.AsStatus()
}
Some of us who write articles could do a better job of making this easier for you too, though. We like to use short forms of certain things, like {} and [] which are ##class(%Library.DynamicObject) and ##class(%Library.DynamicArray). I try to remember to use the latter in my articles just because it makes it easier to find the thing I'm talking about in the documentation. There are cases like that which are technically correct, but make it harder for beginners to learn.







FYI, in your "Building SQL In Strings" section, you can also still use %SQL.Statement like this:
set stmt = ##class(%SQL.Statement).%New() // Note the question mark in the query. set query = "SELECT Name, Age FROM Patient WHERE ID=?" set sc = stmt.%Prepare(query) // You can add error handing here if the above status results in an error // Providing variables to the %Execute method will insert them where the question marks are in the query, in order set rs = stmt.%Execute(id)