2016-12-08 7 views
1

私のタイトルはあまり有益ではないと私は理解しています。ビジュアルベーシックでは、コードがtryステートメントを渡す前にコードを実行し、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 

それはエラーがあっても「データが正常に追加」と言う実行されるたびに次のように

私のコードです。

Try 
    A 
Catch SomeExceptionThrownByA 
    B 
End Try 

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

右:

+0

これはTry/Catchの目的です。これにより、エラーを処理し、エラーを処理せずに処理を続けることができます。 2回目のTry/Catchを最初のものに移動します。目的の効果を得ようとします。 –

+0

ありがとう、それは問題を解決しました。皮肉なことに私はあなたの答えを投稿したのと同じように試みました。いずれにしても助けていただきありがとうございます。 – IsaSca

+0

@IsaSca:私はそのオプションのファンではありません。 – Heinzi

答えて

1

ので、一言で言えば、あなたは次のような問題がありますか?


これを解決する方法はほとんどありません。 、あなたのケースでは、あなたがすべて次のコードをスキップしたいので、一番簡単なの一つは次のようになります。

オプション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 

それはTry-Catchブロックのショートを保持するので、私は、それ以外の場合はオプション1、可能な場合、およびオプション3を好むだろう。それは

  • Catchブロックが(代わりにブロックCの)ブロックAでエラーをキャッチすることになっていること、それはあまり明らかになり、Cもできれば
  • 物事が複雑になりますので、私は、本当にオプション2好きではありません投げるSomeExceptionThrownByA
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 
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 
    ... 

私は、これはあなたがしたい、または他のデータを検証する必要があるときに何が起こるかと言う理由は?バリデーションを分離することで、より柔軟性を持たせ、コードをよりきれいに保つことができます。

+0

あなたの返事をありがとう、私はすぐにこれをテストします – IsaSca

+0

それは最高の答えです。検証処理と例外処理を混在させてはいけません。 –

0

データ検証に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 
関連する問題