what is the point of a finally block
因此,我认为
这是困扰我的代码:
1 2 3 4 5 6 7 | private void ASLogoff_Click(object sender, RoutedEventArgs e) { MainWindow MW = new MainWindow(); try { MW.Show(); }// This throws an InvalidOperationException finally{ Close(); }//This code never executes } |
如果满足某些条件并引发错误,则主窗口将自动关闭,即使发生这种情况,我也希望close语句运行。show()抛出一个invalidOperationException,它将中断程序而不是执行finally语句。
1 2 3 4 5 6 7 8 9 10 11 |
msdn.microsoft.com HTTPS:/ / / /图书馆/销售额zwc8s4fz.aspx explains恩很好:P></
By using a finally block, you can clean up any resources that are
allocated in a try block, and you can run code even if an exception
occurs in the try block. Typically, the statements of a finally block
run when control leaves a try statement. The transfer of control can
occur as a result of normal execution, of execution of a break,
continue, goto, or return statement, or of propagation of an exception
out of the try statement.
我知道