0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaにおける例外の分類と使い分け

Posted at

■ 非検査例外(Unchecked Exception)

  • 代表例NullPointerExceptionIllegalArgumentExceptionIndexOutOfBoundsExceptionなど
  • 特徴
    • コンパイル時に例外処理を強制されない(try-catchthrowsが不要)
    • プログラムのバグや設計ミスが原因で発生するケースが多い
  • 対応方針
    • 発生を前提にせず、**事前にチェックして防ぐ(バリデーションなど)**ことが基本
    • 発生した場合は、リカバリよりも修正対象として扱うことが多い

■ 検査例外(Checked Exception)

  • 代表例IOExceptionSQLExceptionParseExceptionなど
  • 特徴
    • コンパイル時に例外処理が必須(try-catchまたはthrowsが必要)
    • 外部環境やI/Oに依存する処理に対して用いられる
  • 対応方針
    • try-catchでハンドリングするか、呼び出し元に投げる(throws
    • 呼び出し側に「この処理は失敗する可能性がある」と明示的に知らせる意図

設計段階から「起きるべきでないもの」と「起きうる前提のもの」を切り分ける姿勢が重要

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?