I have a simple function within a class. I'm trying to get increase exception handling by using try/catch. My problem is that the returning from the try/catch is not stopping processing in the function. Here's my code:
class Config {
public static $configarray;
function setConfig($json_path) {
try {
file_get_contents($config_file);
} catch (Exception $e) {
die("Config File not found");
return null;
}
$json = file_get_contents($json_path);
try {
json_decode($json,TRUE);
} catch (Exception $e) {
die("Invalid Config file. Check your JSON.");
return null;
}
self::$configarray = json_decode($json,TRUE);
}
} // End of Class
When I run
Config->setConfig('test.json')
I get these errors:
PHP Warning: file_get_contents(test.json): failed to open stream: No such file or directory in /Config.php on line 30
PHP Warning: file_get_contents(test.json): failed to open stream: No such file or directory in /Config.php on line 36
I ALWAYS want to print "Config File Not Found" if the file is not found. How can I catch the exception and prevent further processing in the function?