4

PHP で例外をキャッチするのに問題があります

これが私のコードです。

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

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

および呼び出されるメソッド

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;
    }
}

その結果:

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

ただし、関数内に try-catch ブロックを配置して関数を呼び出すと、問題なく例外をキャッチできます。

私は何を間違っていますか?

4

3 に答える 3

1

クラス本体のすべてを見ることはできませんが、クラスのメソッドを使用する場合は、Public ではなく Private にする必要があります。

于 2013-08-12T13:03:22.277 に答える