为什么在C#中捕获并重新抛出异常? [英] Why catch and rethrow an exception in C#?
问题描述
我正在查看文章 C# - 数据传输对象
I'm looking at the article C# - Data Transfer Object on serializable DTOs.
本文包含以下代码:
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;
}
}
文章的其余部分看起来是理智和合理的一个noob),但是try-catch-throw抛出一个WtfException ... 这不完全等同于不处理异常吗?
The rest of the article looks sane and reasonable (to a noob), but that try-catch-throw throws a WtfException... Isn't this exactly equivalent to not handling exceptions at all?
Ergo:
public static string SerializeDTO(DTO dto) {
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
StringWriter sWriter = new StringWriter();
xmlSer.Serialize(sWriter, dto);
return sWriter.ToString();
}
或者我错过了C#中错误处理的一些基本知识?它与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++.
堆栈溢出问题 重新抛出无参数的catch和不做任何事情的区别? 似乎支持我的争论,try-catch-throw是一个no-op。
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)
推荐答案
文章中的代码的方式是邪恶的。 throw ex
将将异常中的调用堆栈重置为此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.
其次,如果你只是抓住并重新抛出,我看不到附加值,上面的代码示例(或者,给定 throw ex
bit,甚至更好)没有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屋!