gpt4 book ai didi

vb.net - 退出父子

转载 作者:行者123 更新时间:2023-12-03 07:54:48 27 4
gpt4 key购买 nike

我有点困惑如何处理我正在编写的VB .net应用程序中的错误。

这就是我所拥有的:

Private Sub Func1()
Try
'stuff that could raise an error

Catch ex As Exception
MsgBox("There is an error" & ex)
End
End Try

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Func1()
'a lot of stuff that will also raise an error is Func1 does
End Sub

如果有错误,此代码将退出应用程序。

如果删除“结束”,它将继续并在Sub Button1_click中引发多个错误。

因此,我需要在Func1中放置一些内容,这些内容可能会中断Sub Button1_click的执行。

我可以放置Exit Sub,但我有很多Subs都使用此Func1,所以我更喜欢从Func1中完成此操作的方法。

最佳答案

如果不想在Func1失败的情况下继续执行Button1_Click,则将Func1更改为 bool(boolean) 函数

Private Function Func1() As Boolean
Try
'stuff that could raise an error

Return True
Catch ex As Exception
MsgBox("There is an error" & ex)
return False
End Try

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
if Func1() = True Then
'a lot of stuff that will also raise an error is Func1 does
End If
End Sub

关于vb.net - 退出父子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com