[VB.NET] How to catch an error? ("try" statement etc.)
3 replies, posted
Hello.
I thought it would be fun to code a base-64 en-/decryption application while I'm bored, but it occurred to me it would not be happy if an invalid array is entered and attempted to be decrypted.
What I currently have:
[code]
Public Function ToBase64(ByVal data() As Byte) As String
If data Is Nothing Then Throw New ArgumentNullException("data")
Return Convert.ToBase64String(data)
End Function
Public Function FromBase64(ByVal base64 As String) As Byte()
If base64 Is Nothing Then Throw New ArgumentNullException("base64")
Return Convert.FromBase64String(base64)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim EncodedString As String = ToBase64(System.Text.Encoding.UTF8.GetBytes(TextBox1.Text))
TextBox1.Text = EncodedString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim DecodedString As String = System.Text.Encoding.UTF8.GetString(FromBase64(TextBox2.Text))
TextBox2.Text = DecodedString
End Sub
[/code]
When I ask it to decrypt an invalid array it appears to be an [B]FormatException[/B] error.
What I am asking you to help me with is creating a "try" statement to catch the error and return a, let's say MsgBox telling "an error occurred make sure it's a valid base-64 array yabba yabba."
I never worked with that statement, perhaps it's time to learn how to.
Thanks.
Try
...
Catch fe As FormatException
MessageBox.Show("Your Message Here")
End Try
Apparently there were two exceptions - I sorted the other one out myself, though, with my new knowledge.
Thanks. I didn't know it was that simple.
[lua]
Try
'your code here
Catch e As Exception
'Error code here, a good one to use is MessageBox.Show
End Try
[/lua]
That's probably the best way to go about it.
Sorry, you need to Log In to post a reply to this thread.