Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
brocker
Visitor

Try / Catch

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
0 Kudos
6 REPLIES 6
TheEndless
Channel Surfer

Re: Try / Catch

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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
jbrave
Channel Surfer

Re: Try / Catch

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.

- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
kbenson
Visitor

Re: Try / Catch

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)
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
brocker
Visitor

Re: Try / Catch

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
0 Kudos
TheEndless
Channel Surfer

Re: Try / Catch

"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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
kbenson
Visitor

Re: Try / Catch

"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.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.