如果代码在传递try语句之前如何运行代码

时间:2016-12-08 14:01:32

标签: vb.net visual-studio-2010

我知道我的标题不是很有用,而且我认为这很难说。在visual basic中,我的目标是在代码传递给try语句之前运行一段代码,但如果它没有通过try语句则不运行它。

我的代码如下:

Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newCustomersRow As DataRow = Form1.Book_StoreDataSet.Customer.NewRow()
        newCustomersRow("Title") = TextBox1.Text()
        newCustomersRow("First Name") = TextBox2.Text()
        newCustomersRow("Last Name") = TextBox3.Text()
        newCustomersRow("Address Line 1") = TextBox4.Text()
        newCustomersRow("Town") = TextBox5.Text()
        newCustomersRow("County") = TextBox6.Text()
        newCustomersRow("Post Code") = TextBox7.Text()
        newCustomersRow("Card Type") = TextBox8.Text()
        newCustomersRow("Card Number") = TextBox9.Text()
        Try
            newCustomersRow("Expiry Date") = TextBox10.Text()
        Catch ex As ArgumentException
            MsgBox("Please enter date like this: DD/MM/YY.")
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
        End Try
        Try
            Form1.Book_StoreDataSet.Customer.Rows.Add(newCustomersRow)
            MsgBox("Data added successfully")
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
        Catch a As ConstraintException
            MsgBox("That Card Number already exists.")
        End Try
    End Sub
End Class

每次运行时都说"数据成功添加"即使有错误。

4 个答案:

答案 0 :(得分:1)

因此,简而言之,您遇到以下问题:

Try
   A
Catch SomeExceptionThrownByA
   B
End Try

C?  ' only run this if there was no exception before

右?

解决这个问题的方法很少。因为,在您的情况下,您想要跳过 all 以下代码,最简单的是:

选项1

Try
   A
Catch SomeExceptionThrownByA
   B
   Return
End Try

C

替代方案是:

选项2

Try
   A
   C
Catch SomeExceptionThrownByA
   B
End Try

选项3

Dim success = False
Try
   A
   success = True
Catch SomeExceptionThrownByA
   B
End Try

If success Then
   C
End If

就个人而言,如果可能的话,我更喜欢选项1,否则选择选项3,因为它会使Try-Catch块保持简短。我不喜欢选项2,因为它

  • 使得Catch块不应该在块A(而不是块C)和
  • 中捕获错误
  • 如果C也可以抛出SomeExceptionThrownByA
  • ,事情会变得复杂

答案 1 :(得分:1)

在我看来,通过将验证推广到不同的方法,您会得到更好的服务。

所以(原谅语法,我的VB已经有一段时间了):

Private Sub Button1_Click(sender As Object, e As EventArgs)     
     var isValid = ValidateData()
     if (!isValid) then 
        MessageBox....
        return
     end if
     try 
        Form1.Book_StoreDataSet.Customer.Rows.Add(newCustomersRow)
        ....
     catch
     ...

我之所以这样说是您希望或需要验证其他数据时会发生什么?通过分离验证,您可以为自己提供更大的灵活性并使代码更清晰。

答案 2 :(得分:0)

你可以嵌套Try / Catch去获得你想要的结果。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim newCustomersRow As DataRow =    Form1.Book_StoreDataSet.Customer.NewRow()
    newCustomersRow("Title") = TextBox1.Text()
    newCustomersRow("First Name") = TextBox2.Text()
    newCustomersRow("Last Name") = TextBox3.Text()
    newCustomersRow("Address Line 1") = TextBox4.Text()
    newCustomersRow("Town") = TextBox5.Text()
    newCustomersRow("County") = TextBox6.Text()
    newCustomersRow("Post Code") = TextBox7.Text()
    newCustomersRow("Card Type") = TextBox8.Text()
    newCustomersRow("Card Number") = TextBox9.Text()
    Try
        newCustomersRow("Expiry Date") = TextBox10.Text()
         Try
             Form1.Book_StoreDataSet.Customer.Rows.Add(newCustomersRow)
             MsgBox("Data added successfully")
             TextBox1.Text = ""
             TextBox2.Text = ""
             TextBox3.Text = ""
             TextBox4.Text = ""
             TextBox5.Text = ""
             TextBox6.Text = ""
             TextBox7.Text = ""
             TextBox8.Text = ""
             TextBox9.Text = ""
             TextBox10.Text = ""
         Catch a As ConstraintException
             MsgBox("That Card Number already exists.")
         End Try

    Catch ex As ArgumentException
        MsgBox("Please enter date like this: DD/MM/YY.")
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        TextBox10.Text = ""
    End Try

End Sub
 End Class

答案 3 :(得分:0)

在我看来,使用Try..Catch..End Try进行数据验证并不理想。当然最好使用Date.TryParse代替下面的代码并使用If .. End If来检查数据集中是否已存在该对象?

说实话,使用这种方法捕捉不正确输入的日期是有缺陷的。如果有人试图在2016年6月8日进入2016年6月8日,则不会被您的有效性测试所捕获。最好使用DateTimePicker代替。

    If Not Date.TryParse(TextBox10.Text, newCustomersRow("Expiry Date")) Then
        MsgBox("Please enter date like this: DD/MM/YY.")
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        TextBox10.Text = ""
    Else
        Try
            Form1.Book_StoreDataSet.Customer.Rows.Add(newCustomersRow)
            MsgBox("Data added successfully")
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
        Catch a As ConstraintException
            MsgBox("That Card Number already exists.")
        End Try
    End If