cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
mat-ski
Level III

Try not executing catch block when JSON parsing fails

 

I see that when Parse JSON fails in a Try, the catch block doesn't seem to get executed. I also see that parsing malformed JSON has been more problematic than this in the past (see this thread), so I grabbed the suggested parsing logic from that thread, which allows me to get the Try to behave as expected, but it seems problematic that Try has this very specific scenario where it doesn't execute its catch.

 

Code:

someVar = Try(
	Parse JSON("{{1}}")
, Print("in catch 1"); 2; ); someVar2 = Try( Throw( "broke!" )
, Print("in catch 2"); 2; ); someVar3 = Try( txt = Log Capture( result = Parse JSON( "{{1}}") ); If( Length( txt ), Throw( txt ) // error message ); result; // parsed JSON , Print("in catch 3"); 2; ); Print("someVar is:"); Print(someVar); Print("someVar2 is:"); Print(someVar2); Print("someVar3 is:"); Print(someVar3);

Output:

Unexpected "{". Perhaps there is a missing }.
Line 1 Column 2: {►{1}}
"in catch 2"
"in catch 3"
"someVar is:"
.
"someVar2 is:"
2
"someVar3 is:"
2

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Try not executing catch block when JSON parsing fails

Try() does not catch the error because Parse JSON() does not throw an error. It prints a message to the log and returns a missing value when there is an error.

Some functions in JMP throw errors; this is usually because the error is serious and needs to be explicitly handled. Some write a log message (like this one) and the JSL continues. Sometimes the failing return code is just a missing value; you can use the ismissing() function to test for it.

I agree that this should have been a throw rather than a message/missing value but I would not expect it to change after this many releases.

 

Craige

View solution in original post

4 REPLIES 4
mat-ski
Level III

Re: Try not executing catch block when JSON parsing fails

(made this comment in error, I don't see a way to delete it)

Craige_Hales
Super User

Re: Try not executing catch block when JSON parsing fails

Try() does not catch the error because Parse JSON() does not throw an error. It prints a message to the log and returns a missing value when there is an error.

Some functions in JMP throw errors; this is usually because the error is serious and needs to be explicitly handled. Some write a log message (like this one) and the JSL continues. Sometimes the failing return code is just a missing value; you can use the ismissing() function to test for it.

I agree that this should have been a throw rather than a message/missing value but I would not expect it to change after this many releases.

 

Craige
mat-ski
Level III

Re: Try not executing catch block when JSON parsing fails

I suppose that makes sense. Just noting for future readers that you're likely better off using Is Associative Array here rather than Is Missing, since Is Missing will throw in case Parse JSON is successful.

 

It would be helpful if this were noted in the scripting index, since parsing in other languages tend to throw for malformed JSON and since this kind of log only exception looks quite similar to a full exception.

Craige_Hales
Super User

Re: Try not executing catch block when JSON parsing fails

@julian  - documenting how it handles bad JSON and best practice for dealing with it. I think @mat-ski  is correct that ismissing has an annoying failure mode for non-numbers.

Craige