使用之间有什么区别
catch(Exception ex)
{
...
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
和使用
catch // might include (Exception)
{
...
throw;
}
Run Code Online (Sandbox Code Playgroud)
Rex*_*x M 12
throw ex从那一点重新抛出异常对象.这通常很糟糕,因为它会破坏导致原始问题的有用调用堆栈信息.
throw从实际抛出的角度释放原始捕获的异常.它会保留到该点的调用堆栈信息,而不是您捕获的点.
catch(Exception)并且catch基本上是相同的东西,除了显然第一个给你异常对象做某事,而第二个没有.但两者都会抓住所有例外情况.它们是不同的catch(SomeKindOfException),它们只捕获特定类型的异常(或从该类型派生的更具体的类型).这适用于以下情况:
try
{
//some file operation
}
catch(FileNotFoundException fnfex)
{
//file not found - we know how to handle this
}
//any other kind of exception,
//which we did not expect and can't know how to handle,
//will not be caught and throw normally
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1467 次 |
| 最近记录: |