おそらく最も重要なのは、チェックされた例外を決して飲み込まないことです。これは、これをしないことを意味します:
try {
...
} catch (IOException e) {
}
それがあなたの意図でない限り。チェック例外を飲み込んでしまうことがあります。それは、チェック例外をどう処理したらよいかわからない、または「throws Exception」句でインターフェイスを汚染したくない (またはできない) ためです。
どうすればよいかわからない場合は、次のようにします。
try {
...
} catch (IOException e) {
throw new RuntimeException(e);
}
もう 1 つ頭に浮かぶのは、例外を確実に処理することです。ファイルの読み取りは次のようになります。
FileInputStream in = null;
try {
in = new FileInputStream(new File("..."));;
// do stuff
} catch (IOException e) {
// deal with it appropriately
} finally {
if (in != null) try { in.close(); } catch (IOException e) { /* swallow this one */ }
}