Hey All,
I’m reading that there is no sort of exception handling in BrightScript, so no Try/Catch blocks. What is the way you all handle exceptions in your code?
Thanks
Bud
Hey All,
I’m reading that there is no sort of exception handling in BrightScript, so no Try/Catch blocks. What is the way you all handle exceptions in your code?
Thanks
Bud
If you have a particularly volatile bit of code, you can use Eval() and/or Run() in conjunction with GetLastRunRuntimeError (and GetLastRunCompileError) to execute it without bringing down your app.
when you are testing, you figure out what is causing the crash by the last line that executed - put print statements after or before each line in the troublesome area and you’ll know what executed last. Then you put a stop statement before the line that failed and check the values of the variables. If there was an “Invalid” variable then find out why it was invalid by backtracking through your code. If it is an “Invalid Brightscript” error, then a variable you were expecting to by of one type is probably another type or invalid, figure out why, again by backtracking through the code. 90% of the issues you encounter are going to by typo’s the rest will be forgetting a “Then” or a “Next” or a parenthesis.
Of course, this assumes you have a telnet session going with your roku box the whole time, which you should.
As TheEndless noted, eval will work, but keep in mind that since strings are not interpolated, there’s only one string quote character, the end of statement character is \n, and you can’t represent that in a string, it’s becomes really painful really fast.
E.g.
quote = CHR(34)
newline = CHR(10)
eval("print "+quote+"quoted string"+quote+newline+"somevar = 1"+newline)
' Or for *slightly* more clarity
code = "print "+quote+"quoted string"+quote+newline
code = code + "somevar = 1"+newline
eval(code)
Thanks!
So does the Eval() return a status then? What does it return if it fails?
I’ve been looking at sample code, and not quite sure what to do with Eval().
Thanks
Bud
“kbenson” wrote:
As TheEndless noted, eval will work, but keep in mind that since strings are not interpolated, there’s only one string quote character, the end of statement character is \n, and you can’t represent that in a string, it’s becomes really painful really fast.E.g.
quote = CHR(34) newline = CHR(10) eval("print "+quote+"quoted string"+quote+newline+"somevar = 1"+newline) ' Or for *slightly* more clarity code = "print "+quote+"quoted string"+quote+newline code = code + "somevar = 1"+newline eval(code)
Or you can store it in a separate file, and use ReadAsciiFile()to feed it to Eval(), so you don’t have to worry about escaping the string.
“TheEndless” wrote:
Or you can store it in a separate file, and use ReadAsciiFile()to feed it to Eval(), so you don’t have to worry about escaping the string.
Yeah, I thought of that, but I’m not sure if it’s a net gain if you have to look in a separate file for a small chunk of code. If it’s easy to turn it into a function, most of the problem goes away. If it’s not, well having the chose to keep your code next to the part it applies to but extremely mangled or push it into a separate text file containing just that code is a painful situation to be in.