0

I'm trying to call a function inside try block and if it fails, catch an exception. My code doesn't work right, what am I doing wrong? Sorry, I'm new on exceptions. Anybody? Any help appreciated :D

What I tried and what doesn't works:

function check ($func) {
    try {
        call_user_func($func);
    } catch (Exception $e) {
        echo "An error occurred.";
    }
}

function test () {
    echo 4/0;
}

check("test");

Returns just "INF" and "Division by zero" error, but should catch that exception and return "An error occurred."

ya_Bob_Jonez
  • 38
  • 1
  • 9

1 Answers1

3

Trying to throw an object that is not will result in a PHP Fatal Error using set_exception_handler().

For more details -

1- https://www.php.net/manual/en/language.exceptions.php#language.exceptions.catch

2- https://www.php.net/manual/en/class.errorexception.php

Try the below code, now error will go to catch.

   function exception_error_handler($severity, $message, $file, $line) {
    if (!(error_reporting() & $severity)) {
        // This error code is not included in error_reporting
        return;
    }

    if($message == 'Division by zero'){
        throw new DivisionByZeroError('Division By Zero Error');
    }else{
        throw new ErrorException($message, 0, $severity, $file, $line);
    }
}

set_error_handler("exception_error_handler");



function check ($func) {
    try {
        call_user_func($func);
    } 


    catch (DivisionByZeroError $e) {
        echo "An Division error occurred - ".$e->getMessage(); //$e->getMessage() will deisplay the error message
    }


    catch (Exception $e) {
        echo "An error occurred - ".$e->getMessage(); //$e->getMessage() will deisplay the error message
    }
}

function test () {


    echo 4/0;

}

check("test");
Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11
  • 1
    set_error_handler used for user-defined error handler function, I modified the code a little bit. In catch, I have added the $e->getMessage () with the error message, to get to know about exception. – Shivendra Singh Jul 30 '19 at 19:36
  • 1
    Glad to help you :) – Shivendra Singh Jul 31 '19 at 07:34
  • Sorry, one more question. Is it possible to catch an ErrorException of a specific type only (e.g. DivisionByZero, OutOfBounds, etc.) with this code you gave me? `catch (DivisionByZeroError $e) {}` doesn't works. – ya_Bob_Jonez Jul 31 '19 at 08:47
  • 1
    On based on message you have need to throw new expansion like 'DivisionByZeroError '. I update the code Please check. – Shivendra Singh Jul 31 '19 at 09:18
  • 1
    Oh, thanks again, how can I give you some rep? So, I need only to check for the message then throw that type? If yes, where can I find these messages like "Division by zero"? I have ~30 types of these Exceptions, that would be a problem... But thanks very much! :D – ya_Bob_Jonez Jul 31 '19 at 15:03
  • 1
    In exception_error_handler function we are using parameter $message. exception message getting from that variable. function exception_error_handler($severity, *$message*, $file, $line) – Shivendra Singh Jul 31 '19 at 15:16
  • Yes, I've got it. But where do I find a list of $message's in different Exceptions? Don't you know? i.e. OutOfBound, OutOfRange, Runtime Exceptions have different messages, aren't they? – ya_Bob_Jonez Jul 31 '19 at 15:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197294/discussion-between-shivendra-singh-and-ya-bob-jonez). – Shivendra Singh Jul 31 '19 at 16:10