1

I'm fairly new to PHP programming and I'm trying to implement some really basic exception handling in my code. However, it seems like "Exception" is not recognised as a class at all and thus no exceptions are caught.

Here is a basic code example, which doesn't work for me:

<?php
try
{
    $x = 0; 
    $y = 1;

    $z = $y / $x;
}
catch (Exception $e)
{ 
    echo "Hello World!"; 
}
?>

When I run this code, I get the following message instead of the expected "Hello World!":

Warning: Division by zero in C:\myFolder\testCase.php on line 7

What I tried so far:

  • use "\Exception" instead of "Exception" to get into the general namespace and maybe find the class there

  • make the namespace known by writing use Exception or similar expressions at the beginning of the code

  • used various other class names instead of "Exception" (e.g. ErrorException, Error, Throwable, DivisionByZeroError ... don't remember all I tried)

If I use the "throw" keyword, it will jump to the catch block just fine. But it should catch Exceptions like Division By Zero automatically, without me having to throw one, right? At least that's what I gathered from searching the web (and this is also the case for other programming languages).

I also thought about using set_exception_handler, but I don't know how I would get specific Exception information that way since I can't use Exception->getMessage(). The only way would be - again - to throw every single exception manually.

What do I have to do in order to make the catch block catch all thrown Exceptions?

If it matters: I'm using PHP Version 7.3.0.

  • It's seems like it is the only way is the one that suggested by VuoriLiikaluoma, unless you check manually $x, by the way, DivisionByZeroError is being thrown only when you module, for example $x % $b. – MyLibary Jan 18 '19 at 15:14
  • @aynber I found that question before and thought it went way more in-depth than I wanted to go. Might still be a duplicate if I got wrong how try-catch works in PHP. I come from a C#/.NET background where the `catch` basically catches every exception that occurs without having to throw them. I wanted something similar for PHP and thought I was doing it wrong. So am I right to assume that `catch` only catches manually thrown exceptions and there is no general exception catching? – GrowingDwarf Jan 18 '19 at 15:24
  • No, it catches all exceptions. However, the error you're getting is a warning, not an exception. It's a different level of error. – aynber Jan 18 '19 at 15:28
  • @aynber Got it. I also need to use the keyword `Error`, which I didn't know existed, because it is not highlighted for me. In that case, it truely is a duplicate. Still, thank you very much for your help! – GrowingDwarf Jan 18 '19 at 15:40

1 Answers1

1

You need to use set_error_handler to catch errors/warnings/notices etc...

Here's an example:

// PHP Errors to Exceptions.
set_error_handler(function (
          $err_severity,
          $err_msg,
          $err_file,
          $err_line,
    array $err_context
) {
    // error was suppressed with the @-operator
    if (0 === error_reporting()) {
        return false;
    }
    switch ($err_severity) {
        case E_WARNING:
            throw new Exception(
                $err_msg,
                0,
                $err_severity,
                $err_file,
                $err_line
            );
    }
});
VuoriLiikaluoma
  • 364
  • 3
  • 11