【发布时间】:2010-10-01 20:59:06
【问题描述】:
当我有这样的方法时:
public static void foo(String param) throws IOException
{
try
{
// some IOoperations
if (param.isEmpty())
{
throw new IOException("param is empty");
}
// some other IOoperations
} catch (Exception e) {
/* handle some possible errors of of the IOoperations */
}
}
当抛出 IOException ("param is empty") 时,它会被该主体中的 try-catch 捕获。但是这个异常是针对这个方法的调用者的。我怎样才能正确地做到这一点?有什么“纯Java”可以做到这一点,还是我必须创建一种不是 IOException 实例的其他类型的异常以避免 try-catch 主体处理它?
我知道你会建议在这种情况下使用IllegalArgumentException。但这是我的情况的一个简化示例。实际上抛出的异常 I 是一个 IOException。
谢谢
【问题讨论】:
标签: java exception-handling try-catch