error handling in VB.net

FadetoBlack

Member
Joined
Mar 11, 2006
Messages
7
Programming Experience
Beginner
Just need help with this program
This is to declare a single dimension array of size 10 and write an error handling (try ..catch) in case the user attempts to access a non existent array element. I can do this without the try catch exception .can u guys help me out in this .Thnks
 

FadetoBlack

Member
Joined
Mar 11, 2006
Messages
7
Programming Experience
Beginner
Its not a homework ,Im learning .Net by myself so the trouble .Its a single dimensional array .I used the conditional statement and the code is posted below .
VB.NET:
Try
           If i <= 10 Then
               MsgBox(a(i))
           End If

       Catch myex As IndexOutOfRangeException
           MsgBox("Array element Not found")

       End Try
 

TechGnome

Well-known member
Joined
May 23, 2005
Messages
896
Programming Experience
10+
IF you check the index against the array.UpperBound and array.LowerBound (it may still be lbound and ubound)... then there's no need for the error handler. Just let the user know that an invalid index has been entered.

-tg
 

Jab

Member
Joined
Feb 14, 2006
Messages
7
Programming Experience
Beginner
yea but i think he is more or less asking why he can't seem to catch the error...which is a good question for beginners like me so maybe someone can post his fixed code :p so I may learn how to catch them as well
 

TechGnome

Well-known member
Joined
May 23, 2005
Messages
896
Programming Experience
10+
He's not getting an error because it won't happen because he's checking for it... right here: If i <= 10 Then ....

In my opinion (and I'm sure I'm alone in this) is that you should attempt to VALIDATE the data FIRST before trying to do anything with it. It's a bad idea (again, IMO) to use error handling as a data validation tool. In .NET exceptions are expensive in time and memory and stack. It's a bad practice that is all too common. It's what I call defensive programing... you're expecting errors to happen and so you defend against them. What should be done is what I call Offensive programming, where you actively prevent the errors from happening in the first place. (yes, I realize that there is a wikipedia entry on this, and yes, my definitions on this are backwards from what's there, but it's a matter of semantice I won't get into.)

Exception handling should really only be fore those extreme situations.

-tg
 

Bob Langlade

Well-known member
Joined
Sep 20, 2005
Messages
115
Location
Saint-Pierre & Miquelon Islands
Programming Experience
3-5
Jab said:
yea but i think he is more or less asking why he can't seem to catch the error...which is a good question for beginners like me so maybe someone can post his fixed code :p so I may learn how to catch them as well

Assuming a is of string type:

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] RetrieveArrayData([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] index [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][INDENT][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][INDENT][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] (a(index))
[/INDENT][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IndexOutOfRangeException
[INDENT]MessageBox.Show(ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#800000]"Index ouf of range"
[/INDENT][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/INDENT][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]

This code will fire an exemption, but as TechGnome mentionned it's a better practice to test your array bounds rather than use Try/Catch when you have total control on your datas.

However when you connect to a server you should use Try/Catch, because datas your are trying to modify could be locked by another user or your server could be inaccessible for some time.
 

FadetoBlack

Member
Joined
Mar 11, 2006
Messages
7
Programming Experience
Beginner
ok i got it , was very simple.Thnks jab and btw i had to use exception cos this was one of the condition given in the question .Anyway thx a lot
 
Top Bottom