尝试在C#中最终捕获 [英] Try catch Finally in C#

查看:143
本文介绍了尝试在C#中最终捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#中的异常处理有一个小疑问.在下面的代码中

I have a small doubt in the Exception handling in C#. In the below code

Try
{

//Throwing Error
}
Catch
{
//Getting Caught
}
Finally
{
int x =3;
}

int x = 5 ;



在抛出Catch中捕获的错误后的尝试中,此后,完成分配x = 3的Finall块.然后执行下一行x = 5.程序没有终止..可能是什么原因?



In Try after throwing an Error which is caught in Catch ,After that Finall Block Executes with Assignment of x=3. and after that next line x=5 is also executed.Program not getting terminated..What might be the reason ?

推荐答案

读到了这句话! >
无论是否存在异常,finally块中的代码都将被执行.当涉及某些内部管理(内存清除")功能时,此功能非常方便,您需要始终像关闭连接一样运行.

现在,我猜你的问题是为什么要这样做:
hi read this!!!

The code inside a finally block will get executed regardless of whether or not there is an exception. This comes in very handy when it comes to certain housekeeping (Memory Clean)functions you need to always run like closing connections.

Now, I''m guessing your question is why you should do this:
try
{
    doSomething();
}
catch
{
    catchSomething();
}
finally
{
    alwaysDoThis();
}


当您可以执行以下操作时:


When you can do this:

try
{
    doSomething();
}
catch
{
    catchSomething();
}

alwaysDoThis();


答案是很多时候catch语句中的代码要么抛出异常,要么脱离当前函数.对于后面的代码,"alwaysDoThis();"如果catch语句中的代码发出返回或引发新异常,则调用将不会执行.

问候
sarva


The answer is that a lot of times the code inside your catch statement will either rethrow an exception or break out of the current function. With the latter code, the "alwaysDoThis();" call won''t execute if the code inside the catch statement issues a return or throws a new exception.

regards
sarva


您提供了一个catch块.结果,程序正常继续.
错误发生后,执行将进入catch块,然后最终运行,然后再运行该行.


异常和异常处理(C#编程指南) [ ^ ]是在许多情况下,可能会引发异常不是通过您的代码直接调用的方法,而是通过调用堆栈中更深的另一种方法.当发生这种情况时,CLR将展开堆栈,为特定的异常类型寻找带有catch块的方法,并且它将执行第一个这样的catch块(如果找到).如果在调用堆栈中的任何地方都找不到合适的catch块,它将终止该过程并向用户显示一条消息."

如果要退出程序,请从catch方法重新抛出错误.
You have provided a catch block. As a result the program continues normally.
After the error, execution enters the catch block and then runs finally and the line thereafter.


One of the statements mentioned at Exceptions and Exception Handling (C# Programming Guide)[^] is "In many cases, an exception may be thrown not by a method that your code has called directly, but by another method further down in the call stack. When this happens, the CLR will unwind the stack, looking for a method with a catch block for the specific exception type, and it will execute the first such catch block that if finds. If it finds no appropriate catch block anywhere in the call stack, it will terminate the process and display a message to the user.".

If you want to exit the program, re-throw the error from the catch method.


案例1:如果您的try块在执行完finally块之后没有得到任何异常代码. 情况2:如果您的try块未获得任何异常,则执行捕获,如果存在&然后转到最后(如果存在)然后终止.

在这种情况下,您的try块可能不会给出任何异常.尝试获得一些例外&调试即可,您了解了.
如果发生异常,执行不会转到int x = 5;
如果未发生异常,则执行int x = 5;
请参考
http://forums.asp.net/t/1713477.aspx/1
Case1:If your try block doesnt get any exception code after finally block get executed.
Case2:If your try block doesnt get any exception execution goes to catch if present & then goes to finally if present then terminates.

In you case your try block may not giving any exception. Try to get some exception & debug then you understand.
Here if exception occurs execution does not go to int x = 5 ;
If exception not occurs then execution goes to int x = 5 ;
Refer this
http://forums.asp.net/t/1713477.aspx/1


这篇关于尝试在C#中最终捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆