LoginSignup
2

More than 5 years have passed since last update.

例外をcatchして更にthrowする場合は前の例外をセットする癖をつける

Last updated at Posted at 2017-11-17

自分が忘れがちなのでメモ。
スタックトレースで発生源までたどれるようになります。

public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )

$previousを設定する

function base_test()
{
    try
    {
        // エラー判定省略
        throw new RuntimeException('エラー詳細1');

        // エラー判定省略
        throw new RuntimeException('エラー詳細2');

        // エラー判定省略
        throw new RuntimeException('エラー詳細3');
    }
    catch (RuntimeException $exception)
    {
        throw new RuntimeException('エラーが発生しました', 0, $exception);
    }
}

function test()
{
    try
    {
        $this->base_test();

        return true;
    }
    catch (RuntimeException $exception)
    {
        // スタックトレースをログに残す
        error_log($exception, 0);
        return false;
    }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2