[英]Is catching DocumentException a good idea?
I have a utility method which reads an xml file and converts to string as below: 我有一个实用程序方法,该方法读取xml文件并转换为字符串,如下所示:
public static String readFile(String xmlFileName) throws IOException, DocumentException{
String xmlMsg = null;
Resource resource = null;
InputStream inputStream = null;
try{
resource = new ClassPathResource(xmlFileName);
inputStream = resource.getInputStream();
SAXReader reader = new SAXReader();
Document doc = reader.read( inputStream );
xmlMsg = doc.asXML();
}finally{
if(inputStream != null){
inputStream.close();
}
}
return xmlMsg;
}
Is it a bad idea if I catch the DocumentException in the above code and rethrow it as below: 如果我在上面的代码中捕获DocumentException并将其重新抛出,是否是个坏主意,如下所示:
public static String readFile(String xmlFileName) throws IOException, DocumentException{
String xmlMsg = null;
Resource resource = null;
InputStream inputStream = null;
try{
resource = new ClassPathResource(xmlFileName);
inputStream = resource.getInputStream();
SAXReader reader = new SAXReader();
Document doc = reader.read( inputStream );
xmlMsg = doc.asXML();
}catch (DocumentException e){
throw new DocumentException("some message");
}finally{
if(inputStream != null){
inputStream.close();
}
}
return xmlMsg;
}
So, is it a bad idea to leave the responsibility of handling the DocumentException to the caller? 因此,将处理DocumentException的责任留给调用者是一个坏主意吗?
No, leaving the caller to handle the Exception
is fine - throw early catch late . 不可以,让调用者处理Exception
可以使早期抛出较晚 。
What I have a problem with is this: 我有一个问题是:
}catch (DocumentException e){
throw new DocumentException("some message");
Why would you catch (DocumentException e)
and then throw a new instance that strips out all useful information ? 为什么要catch (DocumentException e)
,然后抛出一个新实例以剥离所有有用信息 ? You can simply not catch it in the first place and let it percolate up to someone who can handle it. 首先,您根本无法抓住它,而让它渗入可以处理它的人手中。
Also, use Java 7 try-with-resources rather than finally
. 另外,请使用Java 7 try-with-resources,而不要使用finally
。 So, you code should be: 因此,您的代码应为:
public static String readFile(String xmlFileName) throws IOException, DocumentException {
try (final InputStream is = new ClassPathResource(xmlFileName).getInputStream()) {
final SAXReader reader = new SAXReader();
final Document doc = reader.read(inputStream);
return doc.asXML();
}
}
I have removed the variables that are declared as null
then reassigned, I hate this practice and so do many other Java developers - get out of this habit. 我删除了声明为null
然后重新分配的变量,我讨厌这种做法,许多其他Java开发人员也是如此-摆脱这种习惯。 Declare things when you need them and assign them immediately . 在需要时声明它们并立即分配 。 In a garbage collected language the principal of minimum scope is very important. 在垃圾回收语言中,最小范围的原则非常重要。
I have also changed it to return
directly rather than storing the value for some reason. 我还更改了它以直接return
而不是出于某种原因存储值。
catch (DocumentException e){ // here you are catching exception
throw new DocumentException("some message");// now you are changing
// exception message
}
That is a bad practice you have to throw originally thrown exception to upper level then you can handle the Exception
there. 这是一个坏习惯,您必须将最初抛出的异常抛出到更高级别,然后才能在那里处理Exception
。
This way is better 这样比较好
catch (DocumentException e){
throw new DocumentException("some message",e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.