从VB.NET中的Try/catch块转到GOTO [英] GOTO from Try/catch block in VB.NET

查看:105
本文介绍了从VB.NET中的Try/catch块转到GOTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道GOTO是错误的,每次使用它时,小猫都会死掉;)还有一件事让我感到好奇:

如果我在try/catch块中使用GOTO怎么办

 尝试...一些代码...抓住...一些代码...转到标签1结束尝试...如果出现错误,可以跳过更多代码标签1:其余代码 

我是否正确理解try/catch块永远不会在程序中关闭并且可能导致问题?还是不?

似乎也有一些OnError GoTo命令,但我对此没有太多经验.也许可以使用它重写我的代码?

解决方案

我是否正确理解try/catch块永远不会关闭

否,try/catch块将关闭,因为您将其保留.可以跳出try/catch块..>

但是,代码应重写为:

 尝试...一些代码......如果出现错误,可以跳过更多代码抓住...一些代码...结束尝试其余代码 

Yes, I know that GOTO is wrong and every time I use it a kitten dies ;) Still one thing makes me curious:

What if I use GOTO in try/catch block

Try
...some code...
Catch
...some code... 
  GoTo Label1
End Try
...some more code to be skipped if error
Label1:
...rest of code...

Do I understand correctly that the try/catch block will never get closed in the program and it may cause problems? Or not?

There seems to be also some OnError GoTo command but I don't have much experience with it. Maybe my code could be rewriten using it?

解决方案

Do I understand correctly that the try/catch block will never get closed

No, the try/catch block will be closed because you leave it. It is okay to jump out of the try/catch block.

However, the code should be rewritten to:

Try
  ...some code...
  ...some more code to be skipped if error
Catch
  ...some code... 
End Try

...rest of code...

这篇关于从VB.NET中的Try/catch块转到GOTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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