LoginSignup
1
0

More than 3 years have passed since last update.

バラバラな例外をまとめてキャッチする PHP7.1〜

Last updated at Posted at 2020-02-11

前書き

まとめてキャッチできたら楽だよなって思い調べたら、PHP7.1以降なら使えるみたいなので書き方を載せておきます。

PHP7.1以前の書き方

try
{
 //例外ごとにキャッチを書かなけばならない
}
catch (Exception1 $t)
{
   echo $t->getMessage();
}
catch (Exception2 $e)
{
   echo $e->getMessage();
}

PHP7.1以降の書き方

try
{
//キャッチ一つで複数の例外をまとめてキャッチできる
} catch (Exception1 | Exception2 $e) {
   echo $e->getMessage();
}

まとめて書くとコードもスッキリして、かなりいいと思いました。

1
0
0

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
1
0