throw; preserves the original stack trace of the exception, which is stored in the Exception.StackTrace property.
Opposite to that, throw e;
updates the StackTrace property of e.
throw, 保留异常的原始堆栈跟踪,该跟踪存储在异常中。StackTrace属性。相反,抛出e; 更新e的StackTrace属性。
举例说明一下:
int a = 0; int b = 0;
try
{
int z = a / b;
}
catch (Exception ex)
{
throw ex;
}
// 这种 throw ex;会将到现在为止的所有信息清空,认为你catch到的异常已经被处理了,只不过处理过程中又抛出新的异常,从而找不到真正的错误源。
try
{
int z = a / b;
}
catch (Exception ex)
{
throw;
}
try
{
int z = a / b;
}
catch (Exception)
{
throw;
}
//一般情况,正确的使用方法,是上面这两种方式,这样会保留原始的错误信息,
//catch (Exception ex)和 catch (Exception ) 是一样的, 直接Throw就可以了
总结:为了方便问题定位,上面三种try catch 写法,没有特殊逻辑处理推荐: 第二种或者第三种