请考虑以下代码C#代码."finally"块是否执行?
public void DoesThisExecute() {
string ext = "xlsx";
string message = string.Empty;
try {
switch (ext) {
case "xls": message = "Great choice!"; break;
case "csv": message = "Better choice!"; break;
case "exe": message = "Do not try to break me!"; break;
default:
message = "You will not win!";
return;
}
}
catch (Exception) {
// Handle an exception.
}
finally {
MessageBox.Show(message);
}
}
Run Code Online (Sandbox Code Playgroud)
哈,在我写完这篇文章之后,我意识到我本可以在Visual Studio中自己测试过.但是,请随时回答!
是否有适合使用try-finally没有catch块的块的情况?
我看到Finallyin Try .. Catch将始终在try catch块执行的任何部分之后执行.
是否有任何不同,只是跳过该Finally部分,然后在try catch块之外运行它?
Try
'Do something
Catch ex As Exception
'Handle exception
Finally
'Do cleanup
End Try
Run Code Online (Sandbox Code Playgroud)
Try
'Do something
Catch ex As Exception
'Handle exception
End Try
'Do cleanup
Run Code Online (Sandbox Code Playgroud) 另一个面试问题是期待一个真/假答案,我不太确定.
我有一些代码,作为一个轻量级,无阻塞,关键部分.我希望无论在子句_func和子句中发生什么,都保证继续运行,使得其块中的语句将始终执行.cancellationTokenTask.RunExitfinally
是否可以安全地假设下面的finally块,在此过程中没有灾难性故障,将以与最终正常运行的大致相同的保证执行?
if (Enter())
{
Task.Run<T>(_func, cancellationToken)
.ContinueWith((antecedent) =>
{
try
{
antecedent.Wait(cancellationToken);
Interlocked.Exchange<T>(ref _result, antecedent.Result);
}
catch (AggregateException e)
{
Interlocked.Exchange(ref _exceptions, e);
}
catch (OperationCanceledException)
{
ResetState();
}
catch (Exception e)
{
Interlocked.Exchange(ref _exceptions, new AggregateException(e));
}
finally
{
Exit();
}
})
}
Run Code Online (Sandbox Code Playgroud) 一个简单的控制台应用程序,在 Visual Studio 2019、.Net Framework 4.7、Windows 中:
static void Main(string[] args)
{
try
{
Console.WriteLine("In try");
throw new IndexOutOfRangeException();
}
finally
{ *// Surprisingly this part is not being executed.*
Console.WriteLine("In finally");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我确信在没有异常的情况下以及在有异常的情况下都会调用finally块。我在文档中读到:
但是,如果异常未处理,则finally块的执行取决于异常展开操作的触发方式。反过来,这取决于您的计算机的设置方式。
嗯,我很困惑。我是否需要对这个“展开操作”做一些事情,以便在出现未处理的异常时调用finally?
c# finally try-catch console-application unhandled-exception
我有一些部分的相关代码的异常处理两个C#和Java.
正如我在C#中读到的那样,当发生这种StackOverflowException情况时,最终块将不会被执行,因为堆栈上没有空间来执行任何更多的代码,并且当ExecutionEngineException发生时也不会调用它,这可能是由于调用Environment.FailFast().
在我的下面的代码(C#)中,我故意生成StackOverflowException:
class overflow1
{
public void disp()
{
try
{
Console.WriteLine("disp");
disp();
}
catch(Exception e) {
Console.WriteLine(e);
Console.WriteLine("catch");
}
finally {
Console.WriteLine("finalyyy");
}
}
}
class ExceptionHandling
{
static void Main() {
overflow1 s = new overflow1();
s.disp();
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
DISP
DISP
DISP
.
.
.
由于StackOverFlowException,进程终止
正如我所说,如果StackOverflowException发生,因为堆栈上没有空间甚至执行finally块的更多代码.即使catch块的方法也未被调用,可能是由于相同的原因(堆栈中没有剩余空间).
现在我在java中尝试了相同的代码:
public class ExceptionHandlingTest {
static int …Run Code Online (Sandbox Code Playgroud) 可能重复:
最终没有在.net中执行的条件try..finally block
在C#中,finally块会在try,catch中执行,最后是否抛出未处理的异常?
http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java#Finally_Blocks_and_Uncaught_Exceptions指出该finally块并不总是运行.那是对的,对吗?
CLI的ECMA标准(C#从中派生其异常特征)指出异常是在堆栈的两遍搜索中处理的.[13] 第一遍尝试找到匹配的catch块,如果没有找到则终止程序.只有找到匹配的catch块才会执行第二次执行,这将运行插入的finally块.这允许在没有程序状态首先被finally块修改的情况下诊断问题; 它还消除了当程序处于未知状态(例如外部数据损坏或抛出更多异常)时,最终块可能具有不良副作用的风险.
但是,我不需要一个catch来最终执行:
static void Main()
{
try { throw new Exception(); }
finally
{
Console.WriteLine("1");
}
}
Run Code Online (Sandbox Code Playgroud)