PHP exception Handling vs C#
c#, error-handling, php
Solution
You could also convert all your php errors with set_error_handler() and ErrorException into exceptions:
function exception_error_handler($errno, $errstr, $errfile, $errline )
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
$a = 1 / 0;
} catch (ErrorException $e) {
echo $e->getMessage();
}
Problem
this is a really basic question (I hope). Most of the exception handling I have done has been with c#. In c# any code that errors out in a try catch block is dealt with by the catch code. For example ``` try { int divByZero=45/0; } catch(Exception ex) { errorCode.text=ex.message(); } ``` The error would be displayed in errorCode.text. If I were to try and run the same code in php however: ``` try{ $divByZero=45/0; } catch(Exception ex) { echo ex->getMessage(); } ``` The catch code is not run. Based on my limeted understanding, php needs a throw. Doesn't that defeat the entire purpose of error checking? Doesn't this reduce a try catch to an if then statement? if(dividing by zero)throw error Please tell me that I don't have to anticipate every possible error in a try catch with a throw. If I do, is there anyway to make php's error handling behave more like c#?