Using Liberty Basic

Want to learn to program using Liberty Basic? Already programming in Liberty Basic but want to learn more? Follow this experienced programmer (Java) as he teaches himself this wonderful programming language.

 

Tuesday, May 30, 2006

Catching the 'Divide By Zero' Error

In my earlier post 'Simple Math' (dated 30th April 2006), I mentioned that your program would crash if you try to divide something by zero. Correction, your program will crash if you divide something by zero and you don't catch the 'Divide By Zero' error.

Here is an example program that shows you how to catch the 'Divide By Zero' error.



' ==================================
' Catching a Divide By Zero Error
' ==================================
' Description:
' This program demonstrates the use
' of Liberty Basic's 'On Error'
' construct to catch the 'Divide By Zero'
' error condition.
'
' Author: Eddie Meyer
' Date: 29th May 2006

' Constants
' ===========
' The value of the following costant was
' taken from Alyce Watson's ebook (Liberty
' BASIC 4 Companion). Apparently, 11 is
' the error code for the 'Divide By Zero'
' error condition.
Const.DivideByZero = 11

' Variables
' ============

' The first number entered by the user
Num1 = 0
' The second number entered by the user
Num2 = 0

' The result after dividing Num1 by Num2
Result = 0

' Set the 'On Error' handler
On Error Goto [TellUserOff]

' Output some explanatory text to the user
print "This program will ask you for two numbers."
print "It will then display the result of..."
print "Num1 / Num2"
print ""
print "Note: This program will tell you off if you"
print "specify zero (0) as the second of the two"
print "numbers."
print ""

' Get the two numbers
input "Please enter a number: "; Num1
input "Please enter another number: "; Num2
print ""

' Perform the calculation
Result = Num1 / Num2

' Output the result.
print "Num1 divided by Num2 equals "; Result
Goto [end]

[TellUserOff]
' Sanity check - double check that the error
' in question was the 'Divide By Zero' error.
if Err = Const.DivideByZero then
print "You can't divide a number by zero! I"
print "told you this before. What's wrong"
print "with you?"
end if

[end]
end


A crucial point to note about the above program is that we have set an 'On Error' handler. The 'Divide By Zero' error condition is just one kind of error that can potentially occur while your program is running. Another common one is the 'File Not Found' error (which occurs if your program tries to open a file that doesn't exist). Each of the different error conditions that can be handled in your program has a unique error code associated with it. As should be clear from the program above, the error code for the 'Divide By Zero' error is 11. Just for your information, the error code for the 'File Not Found' error is 53. As part of the 'On Error' construct, you need to specify somewhere for the code to go to if an error arises. In our example above, we specified that if an error occurs, we want program execution to jump to the lable named 'TellUserOff'. The rest of the code should be pretty self-explanatory.

I hope you found this post useful.

Please leave me some comments to let me know what you think of it.

Thanks

Eddie

0 Comments:

Post a Comment

<< Home