Error Handling

sunabeda

New member
Joined
Oct 10, 2005
Messages
2
Programming Experience
Beginner
Hello

How do I catch the error which are thrown in the class?

I have a class as follows:

Namespace ErrorClass1
PublicClass Class1
Private mId AsInteger
PublicProperty Id() AsInteger
Get
Return mId
EndGet
Set(ByVal Value AsInteger)
Try
mId = Value
Catch ex As Exception
ThrowNew Exception("Error: Id value should be interget : Source= " + ex.Source + " : Message= " + ex.Message)
EndTry
EndSet
EndProperty
PublicFunction Add() AsInteger
Try
Return mId + 200
Catch ex As Exception
ThrowNew Exception("Error: Add method : Source= " + ex.Source + " : Message= " + ex.Message)
EndTry
EndFunction
EndClass
End
Namespace

This above class I'm importing in a Windows application as follows:

Imports [Error].ErrorClass1
Imports System.Exception
PublicClass Form1
Inherits System.Windows.Forms.Form


FriendWithEvents TextBox1 As System.Windows.Forms.TextBox
FriendWithEvents Button1 As System.Windows.Forms.Button

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x AsNew Class1
Try
x.Id = TextBox1.Text
MessageBox.Show(x.Add.ToString())
Catch ex As Exception
' My question is how do I catch the above marked red color error message while initilizing the above class.

EndTry
EndSub



Please suggest me how do I go about it.
 
first of all when posting code, could you please put the code between the [ code] and [ /code] tags? it makes it easier to read

also to catch exceptions (errors) simply put them in the try/catch blocks that you have

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][SIZE=2][COLOR=#0000ff]Friend[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] WithEvents[/COLOR][/SIZE][SIZE=2][COLOR=#000000] TextBox1 [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Windows.Forms.TextBox[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]Friend [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]WithEvents[/COLOR][/SIZE][SIZE=2] Button1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Button[/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#000000] Button1_Click([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Object, [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.EventArgs) [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#000000] Button1.Click[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]  Try
[/COLOR][/SIZE][/SIZE][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][SIZE=2][SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Class1[/SIZE][/SIZE][/SIZE][/COLOR][/SIZE]
     [SIZE=2][COLOR=#0000ff][SIZE=2][SIZE=2][SIZE=2]x.Id = TextBox1.Text
    MessageBox.Show(x.Add.ToString())
  [/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
     Messagebox.Show(ex.Message) [/SIZE][/SIZE][/SIZE][/COLOR][/SIZE]'displays the error message to the user[SIZE=2][COLOR=#0000ff][SIZE=2][SIZE=2]
  [SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/SIZE][/SIZE][/COLOR][/SIZE]
 
If you mean, how do I catch an exception thrown in another Catch block, you have to have a Try...Catch block outside of the that block to catch it.
VB.NET:
[SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]      Throw [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MyException
[/SIZE][SIZE=2][COLOR=#0000ff]  Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MyException
[/SIZE][SIZE=2][COLOR=#0000ff]      Throw [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Exception("bye")
[/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
    MessageBox.Show("Caught: " & ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]
 
Back
Top