| 目次|前|次 |
IIOExceptionを使用するエラー処理Image I/O APIでは、標準のIOExceptionクラスを独自にサブクラス化したIIOExceptionというクラスを利用します。 IIOExceptionは、ソース・ファイルの解析中に遭遇したすべてのエラー(たとえば、チェックサムの誤りや、ファイル内の特定バイトの値が無効)を通知するために使用されます。これには、読取りプラグインの内部でIOExceptionがスローされる結果になる、実際の入出力エラーも含まれます。
IIOExceptionには、例外の理由を記述したメッセージ(各言語への対応なし)と、そのIIOExceptionの原因となった別のExceptionへの参照(そのような例外が存在する場合)が含まれています。
したがって、アプリケーション・コードで適切なエラー処理を提供しようとすると、次のようなコードになります。
File f = new File("c:\images\myimage.gif");
ImageInputStream iis = null;
try {
iis = ImageIO.createImageInputStream(f);
} catch (IIOException iioe1) {
System.out.println("Unable to create an input stream!");
return;
}
reader.setInput(stream);
try {
reader.read(0, param);
} catch (IIOException iioe2) {
System.out.println("An error occurred during reading: " +
iioe2.getMessage());
Throwable t = iioe2.getCause();
if ((t != null) && (t instanceof IOException)) {
System.out.println("Caused by IOException: " +
t.getMessage());
}
}