如何在 vb.net 中使用 try catch 和 finally 块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16822030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:47:46  来源:igfitidea点击:

How do I use try catch and finally block in vb.net?

vb.netvisual-studio-2008ms-access-2007

提问by Shaikh Nadeem

Below is the code in which I wanted to use try catch and finally block But I am unable to do the same , I am new in programming, Please help me to introduce try catch and finally block in the below code in vb.net, Also help to code for finally block where I will be checking whether connection is open or not , I Connection is open then it should be closed in finally block but after checking . If

下面是我想在其中使用 try catch 和 finally 块的代码但我无法这样做,我是编程新手,请帮助我在 vb.net 中的以下代码中介绍 try catch 和 finally 块,另外帮助为 finally 块编写代码,我将在其中检查连接是否打开,我连接已打开,然后它应该在 finally 块中关闭,但在检查之后。如果

conditions..

状况..

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

回答by Vishal

It is very simple.

这很简单。

Just see the code below.

请看下面的代码。

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

So you can use Try...Catch in your code as follows :

所以你可以在你的代码中使用 Try...Catch 如下:

Dim ValidUser as Boolean = False

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

                ValidUser = true


                Exit For


            End If

        Next

        If ValidUser = True
             MDIParent1.Show()
        Else
             MsgBox("You Are Not A Valid User!!", MsgBoxStyle.Information)
        End If

Catch ex As Exception

    MessageBox.Show(ex.Message)

Finally

    if con.State = ConnectionState.Open then
        con.close()
    End If

    ValidUser = False

End Try