如果代码在通过 try 语句之前,如何 运行 编码
How to run code if the code before it passes a try statement
我知道我的标题信息量不大,而且在我看来很难表达。在 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
每次它 运行 都会显示 "Data added successfully",即使出现错误也是如此。
所以,简而言之,您遇到了以下问题:
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
,事情就变得复杂了。
您可以嵌套 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
在我看来,将验证提取到不同的方法中会更好。
所以(原谅语法,我的 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
...
我之所以这么说,是因为当您想要或需要验证其他数据时会发生什么?通过分离验证,您可以更加灵活并保持代码更简洁。
在我看来,使用 Try..Catch..End Try
进行数据验证并不理想。当然,最好使用 Date.TryParse
而不是像下面的代码,并使用 If .. End If
检查对象是否已存在于数据集中?
老实说,使用这种方法来捕获输入不正确的日期是有缺陷的。如果有人试图将 2016 年 6 月 8 日输入为 08/06/2016,它不会被您的有效性测试捕获。最好改用 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
我知道我的标题信息量不大,而且在我看来很难表达。在 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
每次它 运行 都会显示 "Data added successfully",即使出现错误也是如此。
所以,简而言之,您遇到了以下问题:
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
,事情就变得复杂了。
您可以嵌套 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
在我看来,将验证提取到不同的方法中会更好。
所以(原谅语法,我的 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
...
我之所以这么说,是因为当您想要或需要验证其他数据时会发生什么?通过分离验证,您可以更加灵活并保持代码更简洁。
在我看来,使用 Try..Catch..End Try
进行数据验证并不理想。当然,最好使用 Date.TryParse
而不是像下面的代码,并使用 If .. End If
检查对象是否已存在于数据集中?
老实说,使用这种方法来捕获输入不正确的日期是有缺陷的。如果有人试图将 2016 年 6 月 8 日输入为 08/06/2016,它不会被您的有效性测试捕获。最好改用 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