最后...... [英] And Finally...
问题描述
我只是在思考最后一个异常实际上是什么?b $ b。好吧,我意识到无论是否抛出异常,它总是会被调用,但是不是和下一个
代码行相同吗?这两段代码之间的区别是什么:
尝试
{
WriteFile();
}
catch
{
//亲爱的
Trace.Write("数据丢失);
}
终于
{
//清理代码
关闭文件();
}
和
尝试
{
WriteFile();
}
catch
{
//哦亲爱的
Trace.Write(数据丢失);
}
//清理代码
关闭文件();
干杯
andyt
安迪,
轻松。在代码的第二部分中,如果WriteFile方法抛出
异常,则不会调用CloseFile方法,因为该方法退出了
。 finally子句将保证代码被执行,
在这种情况下看起来很重要,因为你需要清理资源
在方法中初始化。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com
" Andy Turner" <一个*** @ nospam.demon.co.uk>在消息中写道
新闻:uq ******************************** @ 4ax.com ...
我只是在思考实际上异常中最后的内容是什么。好吧,我意识到它总是会被调用,无论是否抛出异常,但是不一定与下一行代码相同吗?这两段代码之间有什么区别:
尝试
{WriteFile();
}
catch
{
//哦亲爱的
Trace.Write(数据丢失);
}
终于
{
//清理代码
CloseFile();
}
和
尝试
{WriteFile();
}
抓住
{
//哦亲爱的
Trace.Write(数据丢失);
}
//清理代码
CloseFile ();
干杯
andyt
如果Trace.Write碰巧抛出另一个异常 - 那么CloseFile将永远不会在第二个场景中执行
。
Andy Turner <一个*** @ nospam.demon.co.uk>在消息中写道
新闻:uq ******************************** @ 4ax.com ...
我只是在思考实际上异常中最后的内容是什么。好吧,我意识到它总是会被调用,无论是否抛出异常,但是不一定与下一行代码相同吗?这两段代码之间有什么区别:
尝试
{WriteFile();
}
catch
{
//哦亲爱的
Trace.Write(数据丢失);
}
终于
{
//清理代码
CloseFile();
}
和
尝试
{WriteFile();
}
抓住
{
//哦亲爱的
Trace.Write(数据丢失);
}
//清理代码
CloseFile ();
干杯
andyt
那么,你如何解决这个问题呢? />
-
Saludos,
Jose Luis礼仪,MCP
Marina <所以***** @ nospam.com>在消息中写道
news:ey ************** @ TK2MSFTNGP10.phx.gbl ...如果Trace.Write发生抛出另一个异常 - 然后在第二个场景中,CloseFile将永远不会被执行。
Andy Turner <一个*** @ nospam.demon.co.uk>在消息中写道
新闻:uq ******************************** @ 4ax.com ... < blockquote class =post_quotes>
我只是在思考最后一个异常实际上是什么。好吧,我意识到它总是会被调用,无论是否抛出异常,但是不一定与下一行代码相同吗?这两段代码之间有什么区别:
尝试
{WriteFile();
}
catch
{
//哦亲爱的
Trace.Write(数据丢失);
}
终于
{
//清理代码
CloseFile();
}
和
尝试
{WriteFile();
}
抓住
{
//哦亲爱的
Trace.Write(数据丢失);
}
//清理代码
CloseFile ();
干杯
andyt
I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn''t that the same as the next
lines of code? What''s the difference between these two bits of code:
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}
and
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
// Cleanup code
CloseFile();
Cheers
andyt
Andy,
Easy. In the second section of code, if the WriteFile method throws an
exception, then the CloseFile method never gets called, as the method is
exited. The finally clause will guarantee that the code gets executed,
which seems important in this case, since you need to clean up resources
initialized in the method.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...
I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn''t that the same as the next
lines of code? What''s the difference between these two bits of code:
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}
and
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
// Cleanup code
CloseFile();
Cheers
andyt
If Trace.Write happens to throw another exception - then CloseFile wil never
gets executed in the second scenario.
"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...
I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn''t that the same as the next
lines of code? What''s the difference between these two bits of code:
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}
and
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
// Cleanup code
CloseFile();
Cheers
andyt
so, how do you fix that?
--
Saludos,
Jose Luis Manners, MCP
"Marina" <so*****@nospam.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...If Trace.Write happens to throw another exception - then CloseFile wil never gets executed in the second scenario.
"Andy Turner" <an***@nospam.demon.co.uk> wrote in message
news:uq********************************@4ax.com...
I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn''t that the same as the next
lines of code? What''s the difference between these two bits of code:
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
finally
{
// cleanup code
CloseFile();
}
and
try
{
WriteFile();
}
catch
{
// Oh dear
Trace.Write ("Data lost");
}
// Cleanup code
CloseFile();
Cheers
andyt
这篇关于最后......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!