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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error trapping example

Status
Not open for further replies.

spiderman0009

Technical User
Feb 19, 2002
23
CA
Can someone explain the part below that I have put in ""..

Why is it 11 and -1 I don't understand?? New to learning programming.. Thank you


Error trapping example

One short example shows you how to put VBScript error-checking to work. In the following code, the AmountPerMonth function calculaates how much money you have to save per nomth to reach a goal to purchase an item You pass the amount of the item and the number of months you're willing to wait, and it returns the amouny of the item and the number of months you're willing to wait, and it returns the amount per month you have to put back. It doesn't take into account any interest on your money, so the process is very simple.

<% DIm Amt
Amt = AmountPerMonth(1000, 0)
%>
<%=Amt%>

<% Function AmountPerMonth(Amount, Months)
On Error Resume Next
Dim MonthlyAmount
MonthlyAmount = Amount / Months
&quot;If Err.Number = 11 Then
AmountPerMonth = -1&quot;
Else
AmountPerMonth = MonthlyAmount
End If
End Function
%>



Computer always croak when you try to divide by zero. It can't be done. But you don't want to show the user an ugly error message. AmountPerMonth uses the On Error Resume Next to stop VBScript fron croaking an then immediately checks for the error after the calculation. A returned value of -1 indicates tat an error occurred because of bad data sent to it.
 
11 is just the number for the error when dividing by zero.
So since the program is doing division, they are checking for division by zero error.

-1 is just a value choosen by the programmer to flag the error.

You could also return a description(The description may suit your needs better) of the error to the call as follows


<% DIm Amt
Amt = AmountPerMonth(1000, 0)
if not isNumeric(Amt) then
response.write &quot;The following error occurred:&quot; & Amt
else
%>
<%=Amt%>

<%end if%>
<% Function AmountPerMonth(Amount, Months)
On Error Resume Next
Dim MonthlyAmount
MonthlyAmount = Amount / Months
If Err.Number = 11 Then
AmountPerMonth = Err.Description
Else
AmountPerMonth = MonthlyAmount
End If
End Function
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top