How to catch an exception from another class method PHP

I'm having trouble catching an exception in PHP

Here's my code.

try {
require $this->get_file_name($action);
}

catch (Exception $e) {
//do something//
}

and the method being called

private function get_file_name($action) {

    $file = '../private/actions/actions_'.$this->group.'.php';

    if (file_exists($file) === false) {
    throw new Exception('The file for this '.$action.' was not found.');
    }

    else {
    return $file;
    }
}

Resulting in:

Fatal error: Uncaught exception 'Exception' with message $action was not found.'
Exception: The file for this $action was not found.

However If I put a try-catch block inside of the function and call the function, I'm able to catch the exception no problem.

What am I doing wrong?

Answers


If you are catching the Exception inside a namespace, make sure that you fall back to the global namespace:

...
}(catch \Exception $e) {
  ...
}...

You can also have a look at the following resources:


I can't see all of the class body but If You want to use method out of the class it should be Public not Private.


Try to check if file You trying to get exists:

var_dump($file = '../private/actions/actions_'.$this->group.'.php');

IMO there is no file in this path


Need Your Help

Unable to out/retval parameter in COM interface method in VC++ 2008

com visual-c++

I want to create a simple COM component in VC++ 2008.

role-based methods for one web service?

web-services soap wsdl roles

I am trying to set up a (for now) really simple web service. By simple, I mean it only has a small amount of actual work to do on the code-side. It only really has one method/function: the client s...