只是一个简单的问题,我似乎无法找到适合在线的答案。Java - 简单的异常处理查询
当我有一个try/catch块,并且想要发现错误时,应该使用System.err.println(...)
还是应该使用throw new Exception(...)
。
例如:
if (null==userList || null==credentials)
System.err.println("Did you call login() first?");
else
{
try
{
currentUser=(String) userList.nextElement();
currentPass=(String) credentials.get(currentUser);
}
catch(NoSuchElementException nsee)
{
System.err.println("Is properties file blank?");
//Or should this be
throw new NoSuchElementException("Is properties file blank?");
nsee.printStackTrace();
}
}
编辑:那么,throw
并不意味着去里面catch
?我可以使用throw
而不是catch
,还是仅用于方法签名? 在这种情况下捕获错误的正确方法是什么,我将试图确保属性文件不是空白并包含值? 此外,为了清楚起见,我的目标是尽可能地向用户清楚错误是什么,所以他们立即知道他们需要修复它们的属性文件。
我认为他们是不同的问题 - System.err主要是用于调试 – Coffee 2012-03-26 20:20:12
一个奇怪的问题。重新投掷的决定似乎与您是否想要登录它或在何处登录无关。 – onnoweb 2012-03-26 20:20:26