PDA

View Full Version : Error handling question


TheRogue
04-13-2004, 07:00 PM
how could i handle a division by zero error
i have a program that calculates the slope of a line but if the line is vertical it gives a division by zero error. how could i make it handle the error in kind of a if division by zero then "line is vertical" im still new so sorry if this is a dumb question

little birdy
04-14-2004, 11:00 PM
Use a Try/Catch block:

Try
' do the division -- slope is (rise / run)
' if i remember highschool math correctly
' [assume some numberic variables, rise, run, and slope
' already exist]

slope = rise / run
Catch ex As DivideByZeroException
' print your "line is vertical" error message
End Try

Essentially, this program tries to perform that division operation. If it is dividing by zero, a DivideByZeroException will be thrown, and then caught by the Catch block. Within the Catch block, do whatever you need to do to remedy the problem, or print an error message, or whatever you want.

(Of course, you could also do a simple If statement, saying If run = 0 Then don't do the calculation. But I chose this route, for whatever reason.)

TheRogue
04-15-2004, 12:06 AM
hey thanks so much!! i was hoping that there was a way to do it