如何在vb.net中使用try catch并最终阻止?

如何在vb.net中使用try catch并最终阻止?,第1张

概述下面是我想使用try catch和finally块的代码但是我无法做同样的事情,我是编程的新手,请帮我介绍一下try catch,最后在vb.net下面的代码中阻塞,另外帮助编写finally块,我将检查连接是否打开,I Connection打开然后应该在finally块中关闭但是在检查之后. 如果 条件.. else 'Try con.Open() adp 下面是我想使用try catch和finally块的代码但是我无法做同样的事情,我是编程的新手,请帮我介绍一下try catch,最后在vb.net下面的代码中阻塞,另外帮助编写finally块,我将检查连接是否打开,I Connection打开然后应该在finally块中关闭但是在检查之后.
如果

条件..

else    'Try        con.open()        adp = New oleDbDataAdapter("select * from Login ",con)        adp.Fill(dt,"Login")        Dim i As Integer        For i = 0 To dt.tables(0).Rows.Count - 1            If (cbType.Text = dt.tables(0).Rows(i).Item(1) And txtUname.Text = dt.tables(0).Rows(i).Item(2) And txtPass.Text = dt.tables(0).Rows(i).Item(3)) Then                MDIParent1.Show()                Exit Sub            End If            '                Catch ex As Exception        Next        MsgBox("You Are Not A ValID User!!",MsgBoxStyle.information)    End If
解决方法 这很简单.

请看下面的代码.

Try    'In this block your program will try to execute your code.    'If it catches any runtime error it will go to the Catch Block straight away without executing the next code in your Try Block.    'If there are no errors then Finally Block will be executed after Try Block. Catch Block will be skipped.Catch    'It will display the errors here.    'So you can use Catch ex as exception.    'If you want to see the errors in MessageBox you can write the below line.    'MessageBox.Show(ex.Message)Finally    'If your program finds errors or not this block will be executed always.End Try

因此,您可以在代码中使用Try … Catch,如下所示:

Dim ValIDUser as Boolean = FalseTry        con.open()        adp = New oleDbDataAdapter("select * from Login ","Login")        Dim i As Integer        For i = 0 To dt.tables(0).Rows.Count - 1            If (cbType.Text = dt.tables(0).Rows(i).Item(1) And txtUname.Text = dt.tables(0).Rows(i).Item(2) And txtPass.Text = dt.tables(0).Rows(i).Item(3)) Then                ValIDUser = true                Exit For            End If        Next        If ValIDUser = True             MDIParent1.Show()        Else             MsgBox("You Are Not A ValID User!!",MsgBoxStyle.information)        End IfCatch ex As Exception    MessageBox.Show(ex.Message)Finally    if con.State = ConnectionState.Open then        con.close()    End If    ValIDUser = FalseEnd Try
总结

以上是内存溢出为你收集整理的如何在vb.net中使用try catch并最终阻止?全部内容,希望文章能够帮你解决如何在vb.net中使用try catch并最终阻止?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1212888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存