在VB.NET中继续下一步? [英] resume next in VB.NET ?

查看:113
本文介绍了在VB.NET中继续下一步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB 6中,我可以执行以下操作:


Sub MySub

出错时goto Err1

: - all我的代码在这里

: - 这是错误发生的地方

: - 这是继续下一个将在Err1之后带给我的地方

退出子


Err1:

msgbox" error =" &安培;错误

简历下一页

结束子


使用上面的代码,当我收到错误时,它没有退出我的

子程序,相反,我会在收到错误后转到下一个语句。

我可以在VB.NET 2005中做同样的事情吗?谢谢。


目前在VB.NET中,这就是我的工作:


Sub MySub

试试

: - 我的代码在这里

Catch ex As Exception

尝试

swError = New StreamWriter( Application.StartupPath&

" \Log.txt",True)

swError.WriteLine(Now&" error ="& ex.Message )

swError.Close()

swError = Nothing

Catch ex2 As Exception

结束尝试

结束尝试

结束子


使用上面的代码,如果我收到错误,它将退出sub并且不会

执行我子程序中的其余代码。

In VB 6 I can do the following:

Sub MySub
on error goto Err1
: --all my codes are here
: --say this is where the error occurs
: --this is where resume next will bring me after Err1
exit sub

Err1:
msgbox "error = " & error
resume next
end sub

With the above codes, when I get an error, it did not exit out of my
subroutine, instead I will go to the next statement after I get the error.
Can I do the same thing in VB.NET 2005 ? Thank you.

Currently in VB.NET, this is what I do:

Sub MySub
try
: --all my codes are here
Catch ex As Exception
Try
swError = New StreamWriter(Application.StartupPath &
"\Log.txt", True)
swError.WriteLine(Now & " error = " & ex.Message)
swError.Close()
swError = Nothing
Catch ex2 As Exception
End Try
End Try
End Sub

With the above code, if I get an error, it will exit the sub and will not
execute the rest of the code in my subroutine.

推荐答案

4月17日14:55,fniles < fni ... @ pfmail.comwrote:
On 17 Apr, 14:55, "fniles" <fni...@pfmail.comwrote:

在VB 6中,我可以执行以下操作:


Sub MySub

错误goto Err1

* *:* - 我的所有代码都在这里

* *:* - 说这是发生错误

* *: - 这是继续下一个将在Err1之后带给我的地方

退出子


Err1:

* * msgbox" error =" &安培;错误

* *简历下一页

结束子


使用上面的代码,当我收到错误时,它没有退出在我的

子程序之后,我会在收到错误后转到下一个语句。

我可以在VB.NET 2005中做同样的事情吗?谢谢。


目前在VB.NET中,这就是我的工作:


Sub MySub

* * * *尝试

* * * *:* - 我的所有代码都在这里

* * * * Catch ex As Exception

* * * * * *尝试

* * * * * * * * swError =新StreamWriter(Application.StartupPath&

" \ Log.txt",True )

* * * * * * * * swError.WriteLine(现在&错误="& ex..Message)

* * * * * * * * swError.Close()

* * * * * * * * swError = Nothing

* * * * * * Catch ex2 As Exception

* * * * * *结束尝试

* * * *结束尝试

结束子


以上代码,如果我收到错误,它将退出sub并且不会执行我的子例程中的其余代码。
In VB 6 I can do the following:

Sub MySub
on error goto Err1
* * : *--all my codes are here
* * : *--say this is where the error occurs
* * : --this is where resume next will bring me after Err1
exit sub

Err1:
* * msgbox "error = " & error
* * resume next
end sub

With the above codes, when I get an error, it did not exit out of my
subroutine, instead I will go to the next statement after I get the error.
Can I do the same thing in VB.NET 2005 ? Thank you.

Currently in VB.NET, this is what I do:

Sub MySub
* * * * try
* * * * : *--all my codes are here
* * * * Catch ex As Exception
* * * * * * Try
* * * * * * * * swError = New StreamWriter(Application.StartupPath &
"\Log.txt", True)
* * * * * * * * swError.WriteLine(Now & " error = " & ex..Message)
* * * * * * * * swError.Close()
* * * * * * * * swError = Nothing
* * * * * * Catch ex2 As Exception
* * * * * * End Try
* * * * End Try
End Sub

With the above code, if I get an error, it will exit the sub and will not
execute the rest of the code in my subroutine.



您应该将Try和Catch放在您怀疑可能导致

错误的所有代码周围。所以


尝试

: - 你需要捕获错误的一行代码

Catch ex2 As Exception

结束尝试


尝试

: - 您需要捕获错误的一行代码

Catch exc as Exception

结束尝试


您还可以输入一个Finally子句,该子句将运行是否有

是否异常!


祝你好运!

You should put Try and Catch around all code you suspect may cause an
error. So

Try
: --one line of code that you need to catch errors on
Catch ex2 As Exception
End Try

Try
: --one line of code that you need to catch errors on
Catch exc as Exception
End Try

You can also put in a Finally clause that will run whether there is an
exception or not!

Good luck!


AFAIK你还有错误恢复下一个在VB 2005 ...


那说我会避免这个。要么你知道错误是什么(这是尝试捕获的

目的)你可以对此做点什么,或者这是一个

未知错误,IMO仍在尝试盲目奔跑会让事情变得更糟......

-

Patrice


" fniles" < fn **** @pfmail.comaécritdansle message de news:
eC ************** @ TK2MSFTNGP05.phx.gbl ...
AFAIK you still have on error resume next in VB 2005...

That said I would avoid this. Either you know what is the error (this is the
purpose of try catch) and you can do something about this or this is an
unknown error and IMO still trying to blindly runs will make things worse...
--
Patrice

"fniles" <fn****@pfmail.coma écrit dans le message de news:
eC**************@TK2MSFTNGP05.phx.gbl...

在VB 6中我可以做到以下几点:


Sub MySub

出错时转到Err1

: - 我的代码在这里

: - 这就是错误发生的地方

: - 这是继续下一个将在Err1之后带给我的地方

退出子


Err1:

msgbox" error =" &安培;错误

简历下一页

结束子


使用上面的代码,当我收到错误时,它没有退出我的

子程序,相反,我会在收到错误后转到下一个语句。

我可以在VB.NET 2005中做同样的事情吗?谢谢。


目前在VB.NET中,这就是我的工作:


Sub MySub

试试

: - 我的代码在这里

Catch ex As Exception

尝试

swError = New StreamWriter( Application.StartupPath&

" \Log.txt",True)

swError.WriteLine(Now&" error ="& ex.Message )

swError.Close()

swError = Nothing

Catch ex2 As Exception

结束尝试

结束尝试

结束子


使用上面的代码,如果我收到错误,它将退出sub并且不会

执行子程序中的其余代码。

In VB 6 I can do the following:

Sub MySub
on error goto Err1
: --all my codes are here
: --say this is where the error occurs
: --this is where resume next will bring me after Err1
exit sub

Err1:
msgbox "error = " & error
resume next
end sub

With the above codes, when I get an error, it did not exit out of my
subroutine, instead I will go to the next statement after I get the error.
Can I do the same thing in VB.NET 2005 ? Thank you.

Currently in VB.NET, this is what I do:

Sub MySub
try
: --all my codes are here
Catch ex As Exception
Try
swError = New StreamWriter(Application.StartupPath &
"\Log.txt", True)
swError.WriteLine(Now & " error = " & ex.Message)
swError.Close()
swError = Nothing
Catch ex2 As Exception
End Try
End Try
End Sub

With the above code, if I get an error, it will exit the sub and will not
execute the rest of the code in my subroutine.



我同意其他响应者的尝试-catch是一个更好的结构。那个

说,在下面两个评论声明的断点上,你想要的行为就是b $ b。发生i\j错误,然后跳转到err1,最后

接下来按顺序恢复。


Sub MySub()

Dim i,j,k As Integer

On Error GoTo err1

k = i \ j

k = k''休息这里第二个

返回

错误1:

k = k''先在这里休息

继续下一个

End Sub


" fniles"写道:
I agree with other responders about try-catch being a better construct. That
said, with breakpoints on the two commented statements below, the behavior
you want happens. The i\j error occurs, then a branch to err1, and finally
resume next back in sequence.

Sub MySub()
Dim i, j, k As Integer
On Error GoTo err1
k = i \ j
k = k '' break here second
Return
err1:
k = k '' break here first
Resume Next
End Sub


"fniles" wrote:

在VB 6中,我可以执行以下操作:


on错误goto Err1

: - 我的代码在这里

: - 这是错误发生的地方

: - 这是其中简历下一个会带我去Err1

退出子


Err1:

msgbox" error =" &安培;错误

简历下一页

结束子


使用上面的代码,当我收到错误时,它没有退出我的

子程序,相反,我会在收到错误后转到下一个语句。

我可以在VB.NET 2005中做同样的事情吗?谢谢。


目前在VB.NET中,这就是我的工作:


Sub MySub

试试

: - 我的代码在这里

Catch ex As Exception

尝试

swError = New StreamWriter( Application.StartupPath&

" \Log.txt",True)

swError.WriteLine(Now&" error ="& ex.Message )

swError.Close()

swError = Nothing

Catch ex2 As Exception

结束尝试

结束尝试

结束子


使用上面的代码,如果我收到错误,它将退出sub并且不会

执行子程序中的其余代码。
In VB 6 I can do the following:

Sub MySub
on error goto Err1
: --all my codes are here
: --say this is where the error occurs
: --this is where resume next will bring me after Err1
exit sub

Err1:
msgbox "error = " & error
resume next
end sub

With the above codes, when I get an error, it did not exit out of my
subroutine, instead I will go to the next statement after I get the error.
Can I do the same thing in VB.NET 2005 ? Thank you.

Currently in VB.NET, this is what I do:

Sub MySub
try
: --all my codes are here
Catch ex As Exception
Try
swError = New StreamWriter(Application.StartupPath &
"\Log.txt", True)
swError.WriteLine(Now & " error = " & ex.Message)
swError.Close()
swError = Nothing
Catch ex2 As Exception
End Try
End Try
End Sub

With the above code, if I get an error, it will exit the sub and will not
execute the rest of the code in my subroutine.


这篇关于在VB.NET中继续下一步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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