私は、呼び出し元に再スローすることなく例外を処理する方法を正確にはわからない十字路にいつも来ているようです。ローカライズされた例外を処理する方法は?
以下の状況を処理するより良い方法はありますか?
private DataHandler retrieveFromGridFS(ObjectId id) throws IOException
{
GridFS gridFS = new GridFS(getDBReference());
GridFSDBFile out = gridFS.find(id);
File temp = File.createTempFile(
(String)out.getMetaData().get("productName"),
(String)out.getMetaData().get("productType"));
out.writeTo(temp);
return new DataHandler(new FileDataSource(temp));
}
上記private
方法はIOException
を投げることができます。
そうよう、この方法を利用して:私はnullを返す危険性がないように、私は再スローをするように強制してい
public DataHandler retrieveProduct(String productId) throws IOException
{
ObjectId id = new ObjectId(productId);
DataHandler handler = null;
try
{
handler = retrieveFromGridFS(id);
}
catch(IOException ex)
{
logger.error(ex);
throw new IOException("A problem occurred retrieving product.");
}
return handler;
}
。
元の例外を捨てないでください。ログファイルからのデバッグを非常に難しくします。新しいIOExceptionをスローします( "製品の取得中に問題が発生しました。"、ex); ' – MeBigFatGuy
+1;間違いなく根本的な原因を取り除くことは、デバッグの邪魔になります。 –