Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Handle Exceptions

Status
Not open for further replies.

jmgrin

MIS
Jan 4, 2002
32
0
0
US
I'm just getting started with VB.net and am finding the switch from VB6 very painful. I am working on a Windows form that has bound fields to an access database. The fields are of type double. I want to include error checking to make sure that characters are not entered into the fields. It doesn't seem like this should be very difficult, but I can't seem to figure out where I need to trap the error.
I've tried handling it in the mycontrol_validating method and also in try-catch code in the mycontrol_click method of my navigation buttons. The error is thrown before these methods are even executed.
Can anyone tell me how to trap this error.
Thanks for any help you can give me.
 
do this:

try

'INSERT CODE HERE

catch ex as exception
'DO STUFF HERE IF ERROR
end try


this will then try to execute the code, and if there is an exception, it will go to the catch block of code. ex is the object that is the exception.


also, you can do this:

if isnumeric(myDouble) then
'Do stuff
else
messagebox.show("OMG - IT'S A NUMBER NOT A LETTER!", "Double Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
end if




hope this helped.


hayt
 
Thanks for your quick reply. I guess my real question was where I put the code that checks for invalid data. It isn't getting executed when I put it in the mycontrol_validating or mycontrol_onclick methods,
so where do I put it so it gets executed before the unhandled exception is thrown?
 
Try adding your error handling code on either the textchange event or when the control loses focus. However, it SHOULD workon the click event also.
 
usually i place my validation code in a click even of a button. what ever processes it, but like pweegar said, you can put it in the texbox.lostfocus event, or whereever you want. it really is up to you. you can put it anywhere where the data is manipulated.
 
Just another idea -- if it's not a lot of validation, put it in the TextChange event -- you can then validate on every keystroke. I don't raise a message box when I do mine, I set the backcolor of the control. You could also use a the ErrorProvider.
 
You need to put your exception handling routine where the code is able to do something about it. Maybe it's in the function where it got raised, maybe it's two callers back.

The idea is *not* to have try..catch blocks in every function (like you had under VB6) that would be your call stack. That functionality is now in the Exception object itself.

The other thing to remember is that raising an exception is a pretty expensive process (the CLR needs to walk the stack, record stuff, etc), so you don't really want to raise them for data problems -- if you can test the variable with a simple if..then..else conditional, that would be much faster.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for all who responded. I was able to trap the invalid data error by putting the code in either the mycontrol_validating or the mycontrol_textChanged events.

I'm starting a new post because I'm not understanding the behavior of vb.net as it executes these events.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top