1

decode_json() terminates my program when the argument is not valid json. How can I keep my program running and handle the error?

Example:

#!/usr/bin/perl -w

use strict;
use JSON;

my      $json='<html></html>';
my      $ticker=decode_json($json);
print $json;

The last print shall be executed.

darsie
  • 147
  • 6
  • possible duplicate of [How can I catch a "failed to decode JSON" error message in Perl?](http://stackoverflow.com/questions/7809740/how-can-i-catch-a-failed-to-decode-json-error-message-in-perl) – MyGGaN Jul 30 '15 at 18:46

1 Answers1

1

This seems like a prime example of a time to wrap the decode_json() in a try catch block.

Doing so will enable you to continue execution, and it also opens up the door to handle the error so that you can have a specific response to the cases where the error occurs.

Community
  • 1
  • 1
Jeffrey Blake
  • 9,659
  • 6
  • 43
  • 65