Catching Errors - KeyNotFoundException

Hi,

Am new to Xojo and have a bit of code that grabs data from a webservice (JSON) and parses it. All good but when I try to get some values I get the KeyNotFoundException.

I then tried to wrap it in a Try Catch construct as I saw that the e type could be KeyNotFoundException. This still fails as a KeyNotFoundException however:

try

if meeting.Value(“trackCondition”) <> nil then
vnuenm=meeting.Value(“trackCondition”)
end if

catch e as KeyNotFoundException
return
end try

Surely the catch should be catching it and I should not be getting a break on execution ?

Thanks in advance,
JJ

It breaks where the exception happens. If you let it advance past that point, you’ll see it drop into the Catch clause.

But I would not rely on the exception for this. Use Lookup, or HasKey for a Dictionary or HasName for a JSONItem.

Thanks Kem. Alternatives noted ?

Why isn’t the exception passed to the catch clause ? This is what I don’t understand.

Thanks
JJ

I addressed that in the first part of my response. Did you step through the code to see where it goes? Because if you do, you’ll see it next goes into the Catch clause.

[quote=425884:@John James]Hi,
Surely the catch should be catching it and I should not be getting a break on execution ?
[/quote]
There’s a setting to control this behavior.

Ah Julua - that’s what I needed ti know. Many thanks ?

JJ

You can also turn it off for a block of code like this:

#pragma BreakOnExceptions false
try
  s = d.Value( "not found" )
catch e as KeyNotFoundException
  return
end try
#pragma BreakOnExceptions default