LoginSignup
2
0

More than 3 years have passed since last update.

javaで複数の例外をまとめてcatchする

Posted at

javaで、複数の例外についてcatchして同じようにログ出力して上に投げるよいう処理をよく見ますが、
冗長で共通化したいなと思っていたので調べてみたらどうやらできるみたいです。参考

catch (IOException ex) {
     logger.log(ex);
     throw ex;
} catch (SQLException ex) {
     logger.log(ex);
     throw ex;
}

|で区切ってあげると下記のような書き方で共通化できます。
少し注意なのが、java7以降でしか使えないようですので、昔のバージョンで開発してる方はご注意ください。

catch (IOException | SQLException ex) {
     logger.log(ex);
     throw ex;
}
2
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
2
0