0

I need some function that will accept a function as the parameter and will run it in try {} catch (Exception $e) {}. If it runs successfully, do nothing, otherwise, throw new Exception. That's something like function-checker, which checks functions if they ran successfully. Anybody can help me or give advice? Thanks for any replies.

The function should work like that:

function itwillfail () {
    echo 10 / 0;
}
check("itwillfail");

Output: Caught exception: Custom exception text here, because it has been thrown as custom. ("check" is that function I need)

What I tried:

function check($func) {
    try {
        call_user_func($func);
    } catch (Exception $e) {
        throw new Exception("Custom text here.");
    }
}

EDIT: More explained: I need to create function, which do the same as "try" and a lot of different "catch"es for different types of exceptions.

2 Answers 2

1

Summarizing your question:

You want a way to call a custom function from a string variable (which you have already figured out that would be via call_user_func($var);.

You then want that function to throw a custom exception error

Confused What is not clear is the reason you would opt to not define your error handler using the set_error_handler function which would effectively do what your asking and set a switch statement to output different messages based on the error generated.

Example The following example is not using a call_user_func but it effectively allows you to write how the error will be handled

<?php 

    function myerror($error_no, $error_msg) { 
        echo "Error: [$error_no] $error_msg "; 
        echo "\n Now Script will end"; 
        die(); 
    }  

    // Setting set_error_handler 
    set_error_handler("myerror"); 

    $a = 10; 
    $b = 0; 

    // Force the error 
    echo($a / $b);
?> 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm very thankful about your help, but let me give you more understandable information what I need by editing my question. (Check Question edited later)
Check "EDIT" in question, please, for more understandable (I hope) explanation what I need.
Alright, got it. I've tried some other way, works for me. Thanks for help! :D
0

Not every function throws an exception when they fail. Many functions, especially ones that have been around for a long time, simply trigger PHP errors rather than exceptions.

To handle those, you would use a custom error handler: https://www.php.net/manual/en/function.set-error-handler.php

So you could set up a custom error handler that would intercept those kinds of failures and throw them as exceptions. The whole point of that function is to do what you're trying to do - handle errors in a custom way.

1 Comment

Thanks, I understand but that's not what I need. I want to create a function that runs another function as the parameter and checks it using try {} catch (){}. See "what I tried" for example, I need that doesn't work. Another example: php functionthatchecksifanotheroneworks (function () { return 5 / 0; });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.