try {
// Do stuff
}
catch (Exception e) {
throw;
}
finally {
// Clean up
}
Run Code Online (Sandbox Code Playgroud)
在上面的块中,finally块是什么时候调用的?在抛出e之前或者最后被召唤然后赶上?
Eri*_*lje 121
在重新抛出e之后(即在执行catch块之后)将调用它
7年后编辑这个 - 一个重要的注意事项是,如果e没有被调用堆栈进一步向上调试或由全局异常处理程序处理,那么该finally块可能永远不会执行.
Mar*_*ell 81
为什么不尝试一下:
outer try
inner try
inner catch
inner finally
outer catch
outer finally
Run Code Online (Sandbox Code Playgroud)
带代码(格式化为垂直空间):
static void Main() {
try {
Console.WriteLine("outer try");
DoIt();
} catch {
Console.WriteLine("outer catch");
// swallow
} finally {
Console.WriteLine("outer finally");
}
}
static void DoIt() {
try {
Console.WriteLine("inner try");
int i = 0;
Console.WriteLine(12 / i); // oops
} catch (Exception e) {
Console.WriteLine("inner catch");
throw e; // or "throw", or "throw anything"
} finally {
Console.WriteLine("inner finally");
}
}
Run Code Online (Sandbox Code Playgroud)
Bra*_*on 32
在阅读完这里的所有答案后,看起来最终答案取决于:
如果在catch块中重新抛出异常,并且该异常被捕获到另一个catch块内,则所有内容都会根据文档执行.
但是,如果重新处理的异常未处理,则finally永远不会执行.
我在VS2010 w/C#4.0中测试了这段代码示例
static void Main()
{
Console.WriteLine("Example 1: re-throw inside of another try block:");
try
{
Console.WriteLine("--outer try");
try
{
Console.WriteLine("----inner try");
throw new Exception();
}
catch
{
Console.WriteLine("----inner catch");
throw;
}
finally
{
Console.WriteLine("----inner finally");
}
}
catch
{
Console.WriteLine("--outer catch");
// swallow
}
finally
{
Console.WriteLine("--outer finally");
}
Console.WriteLine("Huzzah!");
Console.WriteLine();
Console.WriteLine("Example 2: re-throw outside of another try block:");
try
{
Console.WriteLine("--try");
throw new Exception();
}
catch
{
Console.WriteLine("--catch");
throw;
}
finally
{
Console.WriteLine("--finally");
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
示例1:重新抛出另一个try块: -
asse try
---- inner try
---- inner catch
---- inner finally
--outer catch
--outer最后是
Huzzah!示例2:在另一个try块之外重新抛出: -
try
--catch未处理的异常:System.Exception:抛出了类型'System.Exception'的异常.
在C:\ local source\ConsoleApplication1\Program.cs中的ConsoleApplication1.Program.Main()中:第53行
Dan*_*den 23
您的示例与此代码的行为相同:
try {
try {
// Do stuff
} catch(Exception e) {
throw e;
}
} finally {
// Clean up
}
Run Code Online (Sandbox Code Playgroud)
作为一个侧面说明,如果你真正的意思throw e;(即,扔你刚好赶上了同样的异常),它是多好,只是这样做throw;,因为这将保留原始的堆栈跟踪,而不是创建一个新的.
小智 12
如果catch处理程序块中存在未处理的异常,则finally块将被调用为零次
static void Main(string[] args)
{
try
{
Console.WriteLine("in the try");
int d = 0;
int k = 0 / d;
}
catch (Exception e)
{
Console.WriteLine("in the catch");
throw;
}
finally
{
Console.WriteLine("In the finally");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
C:\用户\管理员\文件\ TestExceptionNesting\BIN \发布> TestExceptionNesting.exe
在尝试
在捕获
未处理的异常:System.DivideByZeroException:尝试除以零.在TestExceptionNesting.Program.Main(String [] args)中的C:\ users\administrator\documents\TestExceptionNesting\TestExceptionNesting.cs:第22行
C:\用户\管理员\文件\ TestExceptionNesting\BIN \发布>
今天我在接受采访时被问到这个问题,面试官一直回过头来说"你确定最终没有被召唤吗?" 我不确定这是否意味着一个技巧问题,或者面试官是否有其他想法,并为我编写了错误的代码进行调试,所以我回家试了一下(构建并运行,没有调试器交互),只是为了让我的想法休息.
| 归档时间: |
|
| 查看次数: |
46207 次 |
| 最近记录: |