为什么最后? [英] Why finally?

查看:61
本文介绍了为什么最后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

终于在try-catch-

中终止块的目的是什么?我很困惑,因为如果我正确地读取

定义,这个块似乎是不必要的。

从Visual Studio中考虑以下内容

文档。在第一个foo()中,即使在try块中有一个

异常,

finally块中的语句也会执行。在第二个foo()中,我从finally块中取出了

语句,但它仍然应该执行
,因为它来自整个事件。


那么......最后和最终之间的区别是什么?块和

什么都没有?


void foo()

{

int i = 123;

string s =" some string";

object o = s;


try

{

//转换无效; o包含不是int的字符串

i =(int)o;

}

catch(例外e)

{

Console.WriteLine(" Exception!");

}

终于

{

Console.Write(" i = {0}",i);

}

}


现在,考虑类似的事情:


void foo()

{

int i = 123;

string s =" some string";

object o = s;


try

{

//转换无效; o包含不是int的字符串

i =(int)o;

}

catch(例外e)

{

Console.WriteLine(" Exception!");

}


Console.Write(" i = {0}",i);

}

解决方案

Ken< kr ** **@fnis.com>写道:

最后在try-catch-
中块的目的是什么?我感到困惑,因为如果我正确地阅读
定义,这个块似乎是不必要的。
请考虑Visual Studio
文档中的以下内容。在第一个foo()中,即使try块中存在
异常,
finally块中的语句也会执行。在第二个foo()中,我从finally块中取出了
语句,但它仍然应该执行,因为它是在整个事件之后执行的。

所以......什么是最终与最终之间的区别。什么都没有?




不同之处在于异常*没有被捕获但是被传播

在堆栈中,或当catch块终止方法时(返回或

抛出异常本身)。在你的情况下不会发生这种情况,因为

你正在捕捉异常(好吧,我想如果有一些非符合CLS的异常,它会发生,或者抛出一个无法捕获的异常。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet/

如果回复小组,请不要给我发邮件


Ken,

请考虑以下事项:

尝试
//转换无效; o包含一个字符串而不是int
i =(int)o;
}
catch(ApplicationException e){
Console.WriteLine(" Exception!");
}
最后
{
Console.Write(" i = {0}",i);
}


我只想捕获ApplicationExceptions,其他类型的例外

将在此例程之外处理。


对于任何类型的异常应用程序或其他我希望最终执行




如果你想捕获异常,记录它,那么最后也很有用

重新抛出异常,以便其他人可以处理它。


catch(例外情况)

{

ExceptionHandler(ex) ; //记录异常

throw; //(重新)抛出被捕的异常

}

终于

{

//在这里清理

}


希望这有帮助

Jay


" Ken" < KR **** @ fnis.com>在消息中写道

新闻:01 **************************** @ phx.gbl ...什么终于在try-catch-
中终结的目的是什么?我感到困惑,因为如果我正确地阅读
定义,这个块似乎是不必要的。
请考虑Visual Studio
文档中的以下内容。在第一个foo()中,即使try块中存在
异常,
finally块中的语句也会执行。在第二个foo()中,我从finally块中取出了
语句,但它仍然应该执行,因为它是在整个事件之后执行的。

所以......什么是最终与最终之间的区别。阻止和什么都没有?

void foo()
{i / 123 int = 123;
string s =" some string" ;;
对象o = s;

尝试
//无效转换; o包含一个字符串而不是int
i =(int)o;
}
catch(例外e)
{
Console.WriteLine(" Exception!") ;);
}
最后
{
Console.Write(" i = {0}",i);
}
}

现在,考虑类似的事情:

void foo()
{i / 123 int = 123;
string s =" some string" ;;
对象o = s;

尝试
//无效转换; o包含一个字符串而不是int
i =(int)o;
}
catch(例外e)
{
Console.WriteLine(" Exception!") ;);


Console.Write(" i = {0}",i);
}



Ken,


拥有finally块可确保其中的代码始终为

之前执行功能退出。


例如:


试试

{

//转换无效; o包含不是int的字符串
i =(int)o;

//如果没有异常返回

return; //通常这会退出函数,但是因为

最后{}代码也将被执行。

}

catch(异常) e)

{

Console.WriteLine(" Exception!");

throw e; //这会导致函数退出 - 最后{}

代码将被执行!

}

终于

{

Console.Write(" i = {0}",i);

}

}


最后{}是放置资源清理代码的好地方 - 确保它在返回之前总是被执行$

示例是诸如流,消息传递,信令等的.Close()之类的东西。

最重要的是,如果你使用关键部分,互斥体等等。你

应该将这些对象的释放放在finally {}中,以确保它们被释放,这样你就不会遇到死锁或无限期的资源

锁定问题。

艾琳。


" Ken" < KR **** @ fnis.com>在消息中写道

news:01 **************************** @ phx.gbl ... < blockquote class =post_quotes>最终try-catch-
中块的目的是什么?我感到困惑,因为如果我正确地阅读
定义,这个块似乎是不必要的。
请考虑Visual Studio
文档中的以下内容。在第一个foo()中,即使try块中存在
异常,
finally块中的语句也会执行。在第二个foo()中,我从finally块中取出了
语句,但它仍然应该执行,因为它是在整个事件之后执行的。

所以......什么是最终与最终之间的区别。阻止和什么都没有?

void foo()
{i / 123 int = 123;
string s =" some string" ;;
对象o = s;

尝试
//无效转换; o包含一个字符串而不是int
i =(int)o;
}
catch(例外e)
{
Console.WriteLine(" Exception!") ;);
}
最后
{
Console.Write(" i = {0}",i);
}
}

现在,考虑类似的事情:

void foo()
{i / 123 int = 123;
string s =" some string" ;;
对象o = s;

尝试
//无效转换; o包含一个字符串而不是int
i =(int)o;
}
catch(例外e)
{
Console.WriteLine(" Exception!") ;);


Console.Write(" i = {0}",i);
}



What is the purpose of a finally block in try-catch-
finally? I am confused because, if I am reading the
definition correctly, this block seems unnecessary.
Consider the following from the Visual Studio
documentation. In the first foo(), the statement in the
finally block will execute even though there is an
exception in the try block. In the second foo(), I took
the statement out of the finally block but it should still
execute since it comes after the whole thing.

So... what is the difference between a "finally" block and
nothing at all?

void foo()
{
int i = 123;
string s = "Some string";
object o = s;

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
catch (Exception e)
{
Console.WriteLine("Exception!");
}
finally
{
Console.Write("i = {0}", i);
}
}

Now, consider something similar:

void foo()
{
int i = 123;
string s = "Some string";
object o = s;

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
catch (Exception e)
{
Console.WriteLine("Exception!");
}

Console.Write("i = {0}", i);
}

解决方案

Ken <kr****@fnis.com> wrote:

What is the purpose of a finally block in try-catch-
finally? I am confused because, if I am reading the
definition correctly, this block seems unnecessary.
Consider the following from the Visual Studio
documentation. In the first foo(), the statement in the
finally block will execute even though there is an
exception in the try block. In the second foo(), I took
the statement out of the finally block but it should still
execute since it comes after the whole thing.

So... what is the difference between a "finally" block and
nothing at all?



The difference is when an exception *isn''t* caught, but is propagated
up the stack, or when the catch block terminates the method (returns or
throws an exception itself). This doesn''t happen in your case, because
you''re catching Exception (well, I suppose it will happen if some non-
CLS compliant exception, or an uncatchable exception, is thrown).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Ken,
Consider the following:

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
} catch (ApplicationException e) {
Console.WriteLine("Exception!");
}
finally
{
Console.Write("i = {0}", i);
}
Where I only want to catch ApplicationExceptions, other kind of exceptions
will be handled outside of this routine.

For any kind of exception Application or otherwise I want the finally to be
executed.

Also finally is useful if you want to catch the exception, log it, then
rethrow the exception so others can deal with it.

catch (Exception ex)
{
ExceptionHandler(ex); // logs the exception
throw; // (re) throws the caught exception
}
finally
{
// do clean up here
}

Hope this helps
Jay

"Ken" <kr****@fnis.com> wrote in message
news:01****************************@phx.gbl... What is the purpose of a finally block in try-catch-
finally? I am confused because, if I am reading the
definition correctly, this block seems unnecessary.
Consider the following from the Visual Studio
documentation. In the first foo(), the statement in the
finally block will execute even though there is an
exception in the try block. In the second foo(), I took
the statement out of the finally block but it should still
execute since it comes after the whole thing.

So... what is the difference between a "finally" block and
nothing at all?

void foo()
{
int i = 123;
string s = "Some string";
object o = s;

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
catch (Exception e)
{
Console.WriteLine("Exception!");
}
finally
{
Console.Write("i = {0}", i);
}
}

Now, consider something similar:

void foo()
{
int i = 123;
string s = "Some string";
object o = s;

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
catch (Exception e)
{
Console.WriteLine("Exception!");
}

Console.Write("i = {0}", i);
}



Ken,

Having a finally block ensures that the code inside of it will always be
executed before the function is exited.

example:

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
// if you do not have an exception return
return; // normally this would exit the function, however because
finally{} that code will be executed as well.
}
catch (Exception e)
{
Console.WriteLine("Exception!");
throw e; // this would cause function exit too- with finally{} that
code will be executed!
}
finally
{
Console.Write("i = {0}", i);
}
}

finally{} is a great place to put resource clean up code- ensuring that it
always gets executed before returning!
Examples are things such as .Close() of streams, messaging, signalling, etc.
As most importantly, if you work with critical sections, mutex''s etc. You
should put that release of those objects in a finally{} to ensure that they
are released so you don''t encounter a deadlock, or resource indefinitely
locked issues.
Erin.

"Ken" <kr****@fnis.com> wrote in message
news:01****************************@phx.gbl...

What is the purpose of a finally block in try-catch-
finally? I am confused because, if I am reading the
definition correctly, this block seems unnecessary.
Consider the following from the Visual Studio
documentation. In the first foo(), the statement in the
finally block will execute even though there is an
exception in the try block. In the second foo(), I took
the statement out of the finally block but it should still
execute since it comes after the whole thing.

So... what is the difference between a "finally" block and
nothing at all?

void foo()
{
int i = 123;
string s = "Some string";
object o = s;

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
catch (Exception e)
{
Console.WriteLine("Exception!");
}
finally
{
Console.Write("i = {0}", i);
}
}

Now, consider something similar:

void foo()
{
int i = 123;
string s = "Some string";
object o = s;

try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
catch (Exception e)
{
Console.WriteLine("Exception!");
}

Console.Write("i = {0}", i);
}



这篇关于为什么最后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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