<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Try not executing catch block when JSON parsing fails in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/678994#M86525</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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 &lt;A href="https://community.jmp.com/t5/Discussions/Failsafe-JSON-parsing/m-p/56951/thread-id/31949" target="_blank" rel="noopener"&gt;this thread&lt;/A&gt;), 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;someVar = Try(
	Parse JSON("{{1}}")&lt;BR /&gt;,
	Print("in catch 1");
	2;
);

someVar2 = Try(
	Throw( "broke!" )&lt;BR /&gt;,
	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);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Sep 2023 21:10:47 GMT</pubDate>
    <dc:creator>mat-ski</dc:creator>
    <dc:date>2023-09-18T21:10:47Z</dc:date>
    <item>
      <title>Try not executing catch block when JSON parsing fails</title>
      <link>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/678994#M86525</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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 &lt;A href="https://community.jmp.com/t5/Discussions/Failsafe-JSON-parsing/m-p/56951/thread-id/31949" target="_blank" rel="noopener"&gt;this thread&lt;/A&gt;), 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;someVar = Try(
	Parse JSON("{{1}}")&lt;BR /&gt;,
	Print("in catch 1");
	2;
);

someVar2 = Try(
	Throw( "broke!" )&lt;BR /&gt;,
	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);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 21:10:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/678994#M86525</guid>
      <dc:creator>mat-ski</dc:creator>
      <dc:date>2023-09-18T21:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Try not executing catch block when JSON parsing fails</title>
      <link>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/678999#M86526</link>
      <description>&lt;P&gt;(made this comment in error, I don't see a way to delete it)&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 18:21:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/678999#M86526</guid>
      <dc:creator>mat-ski</dc:creator>
      <dc:date>2023-09-18T18:21:33Z</dc:date>
    </item>
    <item>
      <title>Re: Try not executing catch block when JSON parsing fails</title>
      <link>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/679143#M86532</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 01:04:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/679143#M86532</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-09-19T01:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: Try not executing catch block when JSON parsing fails</title>
      <link>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/679374#M86554</link>
      <description>&lt;P&gt;I suppose that makes sense. Just noting for future readers that you're likely better off using &lt;EM&gt;Is Associative Array&lt;/EM&gt; here rather than &lt;EM&gt;Is Missing&lt;/EM&gt;, since Is Missing will throw in case Parse JSON is successful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 19:11:09 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/679374#M86554</guid>
      <dc:creator>mat-ski</dc:creator>
      <dc:date>2023-09-19T19:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Try not executing catch block when JSON parsing fails</title>
      <link>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/679503#M86557</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2026"&gt;@jules&lt;/a&gt;&amp;nbsp; - documenting how it handles bad JSON and best practice for dealing with it. I think &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/47422"&gt;@mat-ski&lt;/a&gt;&amp;nbsp; is correct that &lt;EM&gt;ismissing&lt;/EM&gt; has an annoying failure mode for non-numbers.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Sep 2023 19:16:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Try-not-executing-catch-block-when-JSON-parsing-fails/m-p/679503#M86557</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-09-19T19:16:48Z</dc:date>
    </item>
  </channel>
</rss>

