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: 

Try .. Catch .. Release

Certain other languages such as JavaScript and PHP have a syntax called "try". Specifically JavaScript has a "try" and "catch" functionality. Implementing such really helps to catch errors without the entire app crashing.

Does Roku's BrightScript have such a "try" functionality?

Thanks.
שלום חבר

Dean at the School of Truth: a part of Autodidact University.

"A programmer is just a tool which converts caffeine into code"
Temet nosce
Vi Veri Veniversum Vivus Vici
Si vis pacem, para bellum
0 Kudos
8 REPLIES 8
Komag
Roku Guru

Re: Try .. Catch .. Release

Not that I'm aware of. But I've gotten used to checking for invalid, such as
IF test.value <> invalid THEN test.value = test.value + 1

or

IF test.value = invalid THEN STOP
(which effectively crashes the program, but in a controlled way 😄 )
0 Kudos
TheEndless
Channel Surfer

Re: Try .. Catch .. Release

Nope. There's no error handling in BrightScript, so you're best to do as Komag suggests and make sure you do validity checks anywhere you might run into an issue.

If there's absolutely no way you can predict the error, then you could use Eval() to execute the code as a string, which will return a success code, but you should avoid this whenever possible, as it inherently leaks memory.
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

Re: Try .. Catch .. Release

Thanks for the information gentlemen. Here is the setup that I have.

I have created an app that verifies access and then builds and displays a grid. All of this is working just as planned up until this point. In fact, even several of the videos play and work correctly just as they should. However, the issue that I am running into is when I try to play anything other than the auto selected item for each row, I get the following error:


Array operation attempted on variable not DIM'd. (runtime error &he7) in ...AArwSoW9/pkg:/source/grid.brs(105)
105: if m.grid_content[msg.GetData()][msg.GetIndex()] <> invalid then


I was getting this error before and tried to implement Komag's suggestion, but I get the same error with the m.grid_content[msg.GetData()][msg.GetIndex()] reference.

For debugging purposes I output the keys of the multidimensional array as I build it with


console_log("grid_content["+Stri(j)+"]["+Stri(i)+"]")


But for some odd reason there's a space in front of the number


34. grid_content[ 0][ 17]
35. grid_content[ 0][ 18]
36. grid_content[ 1][ 0]
37. grid_content[ 1][ 1]


Even using the display code that came with the example shows a space


print "Focused msg: ";msg.GetMessage();"row: ";msg.GetIndex();
print " col: ";msg.GetData()

****PRODUCES****

Focused msg: row: 0 col: 2


So I'm thinking that the space is causing an issue, but I'm not sure how to rectify this or even why it's occurring. Here's the loops that create j and i


for j = 0 to (rsp.row.Count()-1)
list = CreateObject("roArray", rsp.row[j].video.Count(), true)

for i = 0 to (rsp.row[j].video.Count()-1)


So I guess what I was really trying to accomplish here was a way to trap such an error and not have it to cause an app crash.

Thanks.
שלום חבר

Dean at the School of Truth: a part of Autodidact University.

"A programmer is just a tool which converts caffeine into code"
Temet nosce
Vi Veri Veniversum Vivus Vici
Si vis pacem, para bellum
0 Kudos
RokuMarkn
Visitor

Re: Try .. Catch .. Release

No, the space has nothing to do with your problem. That's just the way Stri works, as described in the doc. And an error catching mechanism would not help you, the debugger is already telling you exactly where your program is crashing.

Your problem is m.grid_content is not a two dimensional array. It may not be an array at all. Try printing m.grid_content, then m.grid_content[0], then m.grid_content[0][0].

--Mark
0 Kudos

Re: Try .. Catch .. Release

Thanks for the suggestion. I tried that and here are the results


console_log(m.grid_content)

****PRODUCES****

Type Mismatch. (runtime error &h18) in ...AAen09b6/pkg:/source/main.brs(65)
065: print Stri(m.debug_counter)+". "+message

...

Local Variables:
message &h4010 bsc:roArray, refcnt=2



console_log(m.grid_content[msg.GetIndex()])

****PRODUCES****

Type Mismatch. (runtime error &h18) in ...AA2jrvaZ/pkg:/source/main.brs(65)
065: print Stri(m.debug_counter)+". "+message

...

Local Variables:
message &h0010 bsc:roArray, refcnt=2



console_log(m.grid_content[msg.GetIndex()][msg.GetData()])

****PRODUCES****

Type Mismatch. (runtime error &h18) in ...AAEAkRAV/pkg:/source/main.brs(65)
065: print Stri(m.debug_counter)+". "+message

...

Local Variables:
message &h4010 bsc:roAssociativeArray, refcnt=2
שלום חבר

Dean at the School of Truth: a part of Autodidact University.

"A programmer is just a tool which converts caffeine into code"
Temet nosce
Vi Veri Veniversum Vivus Vici
Si vis pacem, para bellum
0 Kudos
RokuMarkn
Visitor

Re: Try .. Catch .. Release

Use "print", not console_log.

--Mark
0 Kudos

Re: Try .. Catch .. Release

Sorry about that. I used "print" directly and here are the results:


print m.grid_content
<Component: roArray>
<Component: roArray>
<Component: roArray>

print m.grid_content[0]
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>

print m.grid_content[0][2]
shortdescriptionline2: [ShortDescriptionLine2]
shortdescriptionline1: [ShortDescriptionLine1]
stream: <Component: roAssociativeArray>
sdposterurl: http://www.domain.com/5.jpg
description: [Description]
director: [Director]
rating: E
streamformat: mp4
length: 7402
hdposterurl: http://www.domain.com/0.jpg
title: Tuesday 2 of 3
contenttype: episode
actors: <Component: roArray>
שלום חבר

Dean at the School of Truth: a part of Autodidact University.

"A programmer is just a tool which converts caffeine into code"
Temet nosce
Vi Veri Veniversum Vivus Vici
Si vis pacem, para bellum
0 Kudos
Rek
Visitor

Re: Try .. Catch .. Release

"DukeOfMarshall" wrote:
Sorry about that. I used "print" directly and here are the results:


print m.grid_content
<Component: roArray>
<Component: roArray>
<Component: roArray>

print m.grid_content[0]
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>

print m.grid_content[0][2]
shortdescriptionline2: [ShortDescriptionLine2]
shortdescriptionline1: [ShortDescriptionLine1]
stream: <Component: roAssociativeArray>
sdposterurl: http://www.domain.com/5.jpg
description: [Description]
director: [Director]
rating: E
streamformat: mp4
length: 7402
hdposterurl: http://www.domain.com/0.jpg
title: Tuesday 2 of 3
contenttype: episode
actors: <Component: roArray>


Your problem is an attempt to access an element of a non-array:

Array operation attempted on variable not DIM'd. (runtime error &he7) in ...AArwSoW9/pkg:/source/grid.brs(105)
105: if m.grid_content[msg.GetData()][msg.GetIndex()] <> invalid then


In this case, the two possible arrays are: m.grid_content (an array of arrays), and m.grid_content[msg.GetData()] (one of the arrays in m.grid_content).

So, you probably want to do something like:
if type(m.grid_data) <> "roArray" then stop                            ' If this line crashes, m.grid_data is not a valid array
if type(m.grid_data[msg.GetData()]) <> "roArray" then stop ' If this line crashes, m.grid_data[msg.GetData()] is not a valid array


That will at least help you narrow down which array is not valid. You still need to figure out why its not valid though...
0 Kudos