For some reason I always hit the echo command (go read it), even though an error is raised on the line before. This is weird as I explictly wrote that php should catch all errors. (the (Exception $e)
part)
What is happening, and how can I fix it?
$result = '';
for ($k=0; $k < (int)(mb_strlen($plaintext)/2); $k++) {
try {
$i = (strpos($key1, $plaintext[$k]) + $n) % $L1;
$temp = $key[$k]; // Assume an error will raise here
echo "Should never get here, as $key[$k] always should raise an error, as the key was not found
";
$result .= $temp;
} catch (Exception $e) { // (Exception $e) doesn't work either
$result .= $key1[$i];
};
};
Checkout the docs here:
Note:
Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions. However, errors can be simply translated to exceptions with ErrorException.
As per that, you can wrap errors in your handler that throws an exception:
Example #1 Use set_error_handler() to change error messages into ErrorException.
<?php
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
/* Trigger exception */
strpos();
?>