为什么要赶上并重新抛出在C#中的异常? [英] Why catch and rethrow an exception in C#?
问题描述
我在看文章的 C# - 数据传输对象 <在序列化的DTO / EM>。
I'm looking at the article C# - Data Transfer Object on serializable DTOs.
本文包括这块code的:
The article includes this piece of code:
public static string SerializeDTO(DTO dto) {
try {
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
StringWriter sWriter = new StringWriter();
xmlSer.Serialize(sWriter, dto);
return sWriter.ToString();
}
catch(Exception ex) {
throw ex;
}
}
在本文的其余部分看起来理智和合理的(一个小白),但是这的try-catch掷抛出一个WtfException ... 这不是完全等同于根本不处理异常?
人机工程学:
public static string SerializeDTO(DTO dto) {
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
StringWriter sWriter = new StringWriter();
xmlSer.Serialize(sWriter, dto);
return sWriter.ToString();
}
还是我失去了一些关于在C#处理错误的根本?这是pretty多的Java(减去检查异常)一样,不是吗? ......也就是说,他们既精致的C ++。
Or am I missing something fundamental about error handling in C#? It's pretty much the same as Java (minus checked exceptions), isn't it? ... That is, they both refined C++.
该堆栈溢出问题的 之间重新抛出无参数的渔获物和没有做任何事情?的区别 似乎支持我的论点是的try-catch掷是 - 无操作。
The Stack Overflow question The difference between re-throwing parameter-less catch and not doing anything? seems to support my contention that try-catch-throw is-a no-op.
编辑:
只是为了总结的人谁在将来发现这个线程...
Just to summarise for anyone who finds this thread in future...
不要
try {
// Do stuff that might throw an exception
}
catch (Exception e) {
throw e; // This destroys the strack trace information!
}
堆栈跟踪信息可以是至关重要的确定问题的根源!
The stack trace information can be crucial to identifying the root cause of the problem!
DO
try {
// Do stuff that might throw an exception
}
catch (SqlException e) {
// Log it
if (e.ErrorCode != NO_ROW_ERROR) { // filter out NoDataFound.
// Do special cleanup, like maybe closing the "dirty" database connection.
throw; // This preserves the stack trace
}
}
catch (IOException e) {
// Log it
throw;
}
catch (Exception e) {
// Log it
throw new DAOException("Excrement occurred", e); // wrapped & chained exceptions (just like java).
}
finally {
// Normal clean goes here (like closing open files).
}
赶上更具体的例外情况不太具体的前(和Java一样)。
Catch the more specific exceptions before the less specific ones (just like Java).
参考文献:
- MSDN - Exception Handling
- MSDN - try-catch (C# Reference)
推荐答案
第一;该文章中的code做它的方式是邪恶的。 抛出前
将复位异常到这个throw语句是该点的调用堆栈;丢失有关的异常实际上是建立在那里的信息。
First; the way that the code in the article does it is evil. throw ex
will reset the call stack in the exception to the point where this throw statement is; losing the information about where the exception actually was created.
第二,如果你正好赶上并重新抛出这样的,我看不出有任何的附加价值,在code例如上面会一样好(或者,考虑到抛出前
位,甚至更好),而不在try-catch。
Second, if you just catch and re-throw like that, I see no added value, the code example above would be just as good (or, given the throw ex
bit, even better) without the try-catch.
然而,有些情况下,你可能想赶上并重新抛出异常。记录可能是其中之一:
However, there are cases where you might want to catch and rethrow an exception. Logging could be one of them:
try
{
// code that may throw exceptions
}
catch(Exception ex)
{
// add error logging here
throw;
}
这篇关于为什么要赶上并重新抛出在C#中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!