尝试......抓住......最后一块 [英] Try... Catch... Finally blocks

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

问题描述

只是一个快速且可能是愚蠢的问题...


不是代码;


尝试

DoSomething()

抓住e作为例外

HandleError(e)

最后

DoThisAtTheEnd()

结束尝试


功能相同...


尝试

DoSomething ()

抓住e作为例外

HandleError(e)

结束尝试

DoThisAtTheEnd()


我得到的是,我真的没有看到最后声明的重点!在处理捕获和处理之后,代码将继续移动到最终尝试之后的任何内容?!?

您的意见赞赏...:D

____________________________________________

死神

Just a quick and probably daft question...

Isn''t the code;

Try
DoSomething()
Catch e As Exception
HandleError(e)
Finally
DoThisAtTheEnd()
End Try

functionally identical to...

Try
DoSomething()
Catch e As Exception
HandleError(e)
End Try
DoThisAtTheEnd()

What I''m getting at, is that I don''t really see the point of the Finally statement! Surely after the Catching and handling has been dealt with, the code will move on to whatever is after the End Try anyway?!?
Your comments appreciated... :D
____________________________________________
The Grim Reaper

推荐答案

是的,除非你最终在你的HandleError中出现错误:


尝试

DoSomething()

抓住e作为例外

HandleError(e)

最后

''这仍然会运行

DoThisAtTheEnd()

结束尝试

试试

DoSomething()

抓住e作为例外

HandleError(e)

结束尝试


''现在不能运行

DoThisAtTheEnd()


Private Sub HandleError(ByRef ex As Exception)

Dim i As Integer = 1

Dim j as Integer = 0


''故意除以零错误

''模拟不良处理,l ike登录例程不能

''访问数据库服务器(SQL错误?)

Dim k as Integer = i / j


结束子


-

Gregory A. Beamer

MVP; MCP:+ I,SE,SD,DBA


**************************** *******************

在盒子外面思考!

********** *************************************

死神 < gdewdneyatsilvertechcouk>在留言中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

只是一个快速而且可能是愚蠢的问题...


不是代码;


尝试

DoSomething( )

抓住e作为例外

HandleError(e)

最后

DoThisAtTheEnd()

结束尝试


功能相同...


尝试

DoSomething()

Catch e As Exception

HandleError(e)

结束尝试

DoThisAtTheEnd()


我得到的是,我真的没有看到最终

声明的意思!在处理捕获和处理之后,

代码将继续转到最终尝试之后的任何内容?!?

您的意见赞赏......: D $ / $
____________________________________________

死神
Yes, unless you end up with an error inside of your HandleError:

Try
DoSomething()
Catch e As Exception
HandleError(e)
Finally
''This will still run
DoThisAtTheEnd()
End Try
Try
DoSomething()
Catch e As Exception
HandleError(e)
End Try

''This will not run now
DoThisAtTheEnd()

Private Sub HandleError(ByRef ex As Exception)
Dim i As Integer = 1
Dim j as Integer = 0

''Intentional divide by zero error
''simulates bad handling, like logging routine cannot
''access database server (SQL Error?)
Dim k as Integer = i/j

End Sub

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***********************************************
Think Outside the Box!
***********************************************
"Grim Reaper" <gdewdneyatsilvertechcouk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Just a quick and probably daft question...

Isn''t the code;

Try
DoSomething()
Catch e As Exception
HandleError(e)
Finally
DoThisAtTheEnd()
End Try

functionally identical to...

Try
DoSomething()
Catch e As Exception
HandleError(e)
End Try
DoThisAtTheEnd()

What I''m getting at, is that I don''t really see the point of the Finally
statement! Surely after the Catching and handling has been dealt with, the
code will move on to whatever is after the End Try anyway?!?
Your comments appreciated... :D
____________________________________________
The Grim Reaper


啊......我明白了。

我显然还在我的盒子里..(毕竟是星期一)


干杯!

_____________________________________

死神


"牛仔(Gregory A. Beamer)" <否************ @ comcast.netNoSpamM>写在

消息新闻:在************** @ TK2MSFTNGP12.phx.gbl ...
Ahhhh.... I see.
I was obviously still inside my box.. (it is Monday after all)

Cheers!
_____________________________________
The Grim Reaper

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamM> wrote in
message news:On**************@TK2MSFTNGP12.phx.gbl...
是的,除非你结束HandleError中出现错误:

尝试
DoSomething()
抓住e作为例外
HandleError(e)
最后
''这仍然会运行
DoThisAtTheEnd()
结束尝试

尝试
DoSomething()
抓住e作为例外
HandleError(e )
结束尝试

''这将不会立即运行
DoThisAtTheEnd()

私有子HandleError(ByRef ex As Exception)
Dim i As Integer = 1
Dim j as Integer = 0
''意图除以零错误
''模拟不良处理,就像记录日常工作一样不能
' '访问数据库服务器(SQL错误?)
Dim k as Integer = i / j

结束子

- Gregory A. Beamer < MVP; MCP:+ I,SE,SD,DBA

******************************** ***************
在盒子外面思考!
********************** *************************
死神 < gdewdneyatsilvertechcouk>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
只是一个快速且可能是愚蠢的问题......

不是代码;

尝试
DoSomething()
抓住e作为例外
HandleError(e)
最后
DoThisAtTheEnd()
结束尝试

功能完全相同......

尝试
DoSomething()
Catch e作为例外
HandleError(e)
结束尝试
DoThisAtTheEnd()

我得到的是,我不是真的看到了点终于
声明了!在处理完捕获和处理之后,无论如何,代码将转移到最终尝试后的任何内容?!?
您的意见赞赏......:D
____________________________________________
Yes, unless you end up with an error inside of your HandleError:

Try
DoSomething()
Catch e As Exception
HandleError(e)
Finally
''This will still run
DoThisAtTheEnd()
End Try
Try
DoSomething()
Catch e As Exception
HandleError(e)
End Try

''This will not run now
DoThisAtTheEnd()

Private Sub HandleError(ByRef ex As Exception)
Dim i As Integer = 1
Dim j as Integer = 0

''Intentional divide by zero error
''simulates bad handling, like logging routine cannot
''access database server (SQL Error?)
Dim k as Integer = i/j

End Sub

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***********************************************
Think Outside the Box!
***********************************************
"Grim Reaper" <gdewdneyatsilvertechcouk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Just a quick and probably daft question...

Isn''t the code;

Try
DoSomething()
Catch e As Exception
HandleError(e)
Finally
DoThisAtTheEnd()
End Try

functionally identical to...

Try
DoSomething()
Catch e As Exception
HandleError(e)
End Try
DoThisAtTheEnd()

What I''m getting at, is that I don''t really see the point of the Finally
statement! Surely after the Catching and handling has been dealt with,
the code will move on to whatever is after the End Try anyway?!?
Your comments appreciated... :D
____________________________________________
The Grim Reaper



>我得到的是,我真的没有看到最后

声明的意思!


1.在一个Finally子句中当

Try块结束时,任何Dim''变量都将超出范围。


2.如果你退出带有Goto的try块,在Goto之前将执行Finally块

(文档说明了这一点,我从未尝试过)。

> What I''m getting at, is that I don''t really see the point of the Finally
statement!

1. In a Finally clause, any Dim''ed variables will go out of scope when the
Try block finishes.

2. If you exit a try block with a Goto, the Finally block will be executed
before the Goto (Documentation says this, I never tried it out).


这篇关于尝试......抓住......最后一块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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