Is this the framework being buggy or something else?

Some of you might recall my frustrations a few years ago around how Real Studio would simply miss exception handling code on Windows. Even though the code was in a Try/Catch block, when the exception would happen, the application would crash. I had hoped Xojo had fixed that but I’m pretty sure it hasn’t. Still, I’m wondering if someone would look this code over and verify that it’s correct before I start cursing the framework bug gods again.

[code]Dim oHttpSocket as new HttpSecureSocket
Dim serverResponse as String
Dim oJsonItem as JSONItem

oHttpSocket.Secure = True

serverResponse = oHttpSocket.Get(“someurl”, 15)
try
oJsonItem = new JSONItem(serverResponse)
if (oJsonItem.Value(“amount”).StringValue = “1”) then
MsgBox("The value returned was " + oJsonItem.Value(“amount”).StringValue)
end if
catch e as NilObjectException
MsgBox(“There ain’t no freakin object der!”)
catch e as KeyNotFoundException
MsgBox(“I don’t have the key for this door, master.”)
finally
oJsonItem = Nil
oHttpSocket = Nil
end try
[/code]
This code, when run, tries to assign the supposed JSON returned by the server to a new JSONItem. But, when the server doesn’t return valid JSON, the program simply crashes. It SHOULD trigger one of the exceptions, but it doesn’t.

Am I doing this wrong or are exceptions still useless on Windows?

I don’t see that you are catching JSONException. Does the catch work with this addition below?

catch e As JSONException MsgBox("JSON exception!")

Hi Paul,

No, it doesn’t. It looks like the exception being raised is a NilObjectException. As you can see, I specifically have a catch for that exception in my code. But the application is saying “An exception of class NilObjectException was not handled. The application must close”. But, as you can see, i’m catching it!

Any ideas? This is (and has been) driving me nuts!

@Anthony Papillion - If you run this code in debug mode, does the debugger stop somewhere?

Can you give us an example of what the server returns as badly-formed json?

You should also follow Paul’s advice to catch JSONExceptions. If the server returns a value that is not allowed (like a string value without quotes), you’ll definitely get one of those.

Lastly, I’m surprised that you are able to use the same variable for each of the exception blocks. You might try naming each one differently… i.e. noe as NilObjectException, knfoe as KeyNotFoundException, etc…

Start by move the call to the server into the try/catch structure.

Next assume nothing - check what was returned from the call before you use it

And probably the best thing to do - don’t program by exception, it leads to exactly the kind of situation you are experiencing.

A sample of what the server returns would be useful

This code catches the exception fine

[code]Dim serverResponse as String
Dim oJsonItem as JSONItem

serverResponse ="{"“Name”":"“John Doe”","“Age”":32,"“Kids”":["“Jonah”","“John Jr”"],"“Married”":true,"“Spouse”":"“Jane Doe”"}"
try
oJsonItem = new JSONItem(serverResponse)
if (oJsonItem.Value(“amount”).StringValue = “1”) then
MsgBox("The value returned was " + oJsonItem.Value(“amount”).StringValue)
end if
catch e as NilObjectException
MsgBox(“There ain’t no freakin object der!”)
catch e as KeyNotFoundException
MsgBox(“I don’t have the key for this door, master.”)
finally
oJsonItem = Nil
end try[/code]

Greg[quote=59021:@Greg O’Lone]@Anthony Papillion - If you run this code in debug mode, does the debugger stop somewhere?

[/quote]

Yes, it stops when I try to retreive the value of ‘amount’ from the JSON object. Which, since the server isn’t returning JSON, is expected.

Greg[quote=59021:@Greg O’Lone]
Can you give us an example of what the server returns as badly-formed json?
[/quote]

I misused the term ‘badly formed Json’. What I should have said is ‘no json’, Generally, if this server gets a request to the API that isn’t correct, it will simply return a 404 error. So, instead of JSON, it returns standard HTML wrapping around the error details.

Greg[quote=59021:@Greg O’Lone]
You should also follow Paul’s advice to catch JSONExceptions. If the server returns a value that is not allowed (like a string value without quotes), you’ll definitely get one of those.
[/quote]

I did. But the exception being thrown actually ISN’T a JSONException (surprisingly). It’s a NilObjectException, according to the compiler.

Greg[quote=59021:@Greg O’Lone]
Lastly, I’m surprised that you are able to use the same variable for each of the exception blocks. You might try naming each one differently… i.e. noe as NilObjectException, knfoe as KeyNotFoundException, etc…[/quote]

Hmm, I’d think this is normal isn’t it? The variable only gets assigned and defined once depending on what exception is thrown. But I might try using different names just for the heck of it and see if that makes a difference.

[quote=59025:@James Dooley]Start by move the call to the server into the try/catch structure.

Next assume nothing - check what was returned from the call before you use it
[/quote]

I know something is being returned by the call. In fact, I know what is being returned by the call: HTML instead of JSON. But I would think that should either throw a JSONException instead of just crashing.

[quote=59025:@James Dooley]
And probably the best thing to do - don’t program by exception, it leads to exactly the kind of situation you are experiencing.[/quote]

Hmm, do you have a reason for that? Exceptions are the ‘accepted’ way to handle errors in many languages. What’s bad about them? If you don’t use them, your application tends to just crash when something bad happens.

It’s normal. The scope of a variable declared with the catch statement is local to the catch block.

Instead of playing a guessing game on the forum, could you file a bug report with a self-contained sample project?

[quote=59034:@Joe Ranieri]It’s normal. The scope of a variable declared with the catch statement is local to the catch block.

Instead of playing a guessing game on the forum, could you file a bug report with a self-contained sample project?[/quote]

Well, I would love to file a feedback case on this. But I can’t actually get the feedback system to download and install. I click Feedback, it tells me it’s not installed, it asks me if I want to download and install it then…nothing.

Anthony

Feedback is available on this page in the third section from the top. http://www.xojo.com/download/extras.php

Many thanks! One bug report, coming up!

You’re welcome. I had the same problem at first, took me about an hour of searching to finally find it.

Well, forget that. I’m not a license holder yet so I can’t file feedback cases sigh

Can you put the example on dropbox or something similar? I’ll file the bug report if it turns out there is one.

Sure, I’ll actually put the whole project on my dropbox. One sec

No I’m not saying don’t use try/catch mechanisms, I’m saying don’t use them as means of controlling the normal flow of your program.

This is exactly what I’m talking about - in this case you were expecting someone else’s error handling process down the line to operate in a certain way and it did not work out so well for you… If you know that there is a chance that something can go wrong then deal with it, as you are a better position to handle it than anyone else. It means that you can return more meaning full error messages and of course if the other programmer decides to fix his error handling process and changes behaviour it will not have as big an impact on you.

The best way to write solid code is to handle the situations you know are likely to occur and use the try/catch to handle the rest. If you know there is a potential for your call to return HTML, then watch out for it and die gracefully rather than hope that the system will through you the right error.

Any chance you’ve had time to put the project up and could give me a link?

Anthony, what happens when you add a catch without an exception variable at the end of the try/catch block? Does the program still crash even after adding this catch?

  Dim oHttpSocket as new HttpSecureSocket
  Dim serverResponse as String
  Dim oJsonItem as JSONItem
  
  oHttpSocket.Secure = True
  
  serverResponse = oHttpSocket.Get("someurl", 15)
  try
    oJsonItem = new JSONItem(serverResponse)
    if (oJsonItem.Value("amount").StringValue = "1") then
      MsgBox("The value returned was " + oJsonItem.Value("amount").StringValue)
    end if
  catch e as NilObjectException
    MsgBox("There ain't no freakin object der!")
  catch e as KeyNotFoundException
    MsgBox("I don't have the key for this door, master.")
  catch
    MsgBox("Some unexpected exception occured.")
  finally
    oJsonItem = Nil
    oHttpSocket = Nil
  end try