JSON error handling

This is my code for capturing an SMS message list from Twilio. It works, most of the time. Periodically the .Load method will have an error, I assume the string is invalid JSON. Perhaps due to my cheezy pre-processing of the returned text.

In any case, I am getting a JSONException, but adding the exception handler, didn’t handle the exception. Even though in the IDE the line it stopped on is showing the error to be JSONException.
Another odd thing, even though I think the Exception syntax is correct, and it compiles no problem, the IDE shows a couple of little lines in red that normally indicate that a block is not closed properly.

Any ideas why the exception handler doesn’t work?

Function SmsListGet() As SmsCls()
#If TargetWin32 Then
#Pragma Error “This example only works on OS X and Linux due to the use of curl from the shell.”

Dim accountID As String = App.TWILIO_ID
Dim authToken As String = App.TWILIO_AUTH
Dim command As String
Dim sh As New Shell
Dim jsnArray As New JSONItem
Dim jsn As JSONItem
Dim i As Integer
Dim SmsList(-1) As SmsCls
Dim jsnText As String

command = “curl -X GET 'https://api.twilio.com/2010-04-01/Accounts/” + accountID _

  • "/Messages.json’ -u " + accountID + “:” + authToken
    jsnText = sh.Result

i = Instr(jsnText,"[") // cheesy way to get just the array, not the wrapping
If i >= 0 Then
jsnText = Right(jsnText,Len(jsnText) - i + 1)
i = Instr(jsnText,"]")
If i >= 0 Then
jsnText = Left(jsnText,i)
End If
End If
jsnArray.Load jsnText

If jsnArray.IsArray Then
For i = 0 To jsnArray.Count - 1
jsn = jsnArray(i)
If jsn.Value(“status”) = “received” Then
Dim sms As New SmsCls
sms.body = jsn.Value(“body”)
sms.from = jsn.Value(“from”)
sms.id = jsn.Value(“sid”)
smsList.Append sms
End If
End If

Return smsList

Exception err As NilObjectException
Return Nil
Exception err As JSONException
Return Nil

End Function

In debug mode, the IDE will still stop at the exceptions. You can either press run to continue or prefix the try block with

#pragma BreakOnExceptions False

Thanks Greg!

Be sure to follow that up with:

#pragma BreakOnExceptions Default

That will restore your menu setting.

[quote=170369:@Kem Tekinay]Be sure to follow that up with:

#pragma BreakOnExceptions Default

That will restore your menu setting.[/quote]

It’s worth noting that it will also get reset at the end of the scope the pragma is in.