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

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.
 
are you sure it should be 11, and not 1? a value of 1 (as opposed to 0) returned by the error should surely indicate that an error has occurred.

This could be completely wrong as I am only a newbie to coding myself, and know more about PHP.

Hope this is of some help Nick (Web Designer)


nick@retrographics.co.uk
 
Yes, I am sure.. it is 11 still learning, but that is the right code above.

Anyone else have any ideas???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top