Here we can see how try and catch works with simple example:

 <?php

function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
//return "Division error";
}
return 1/$x;
}

try {
echo inverse(5) . "n";
echo inverse(0) . "n";
} catch (Exception $jay) {
echo 'I am safely catched: ',  $jay->getMessage(), "n";
}

// Continue execution
echo "Hello Worldn";
?>

Share on Facebook