Jump to content

Liberty BASIC -- Error Handling

- - - - -

  • Please log in to reply
No replies to this topic

#1
Irfan_A

Irfan_A

    Programmer

  • Members
  • PipPipPipPip
  • 186 posts
Outline : Objective-Requirement-Let's Coding-Mini Quiz

Objective:
Error Handling in Liberty BASIC with ON ERROR GOTO [branchLabel].
Requirement:
Liberty BASIC (i use Liberty BASIC 4.03), or you can use Just BASIC (FREE).
Let's Coding:
Liberty BASIC have special variables, both are case-sensitive, the first is Err. Err is hold numeric values describing what sort error that happened. And the second is Err$. Err$ is hold string values describing what sort error that happened. Let's we always use dollar sign ($) for describing string.

Sort list of error code is given below. Note that some errors don't have numeric values, so in this case Err will be displayed zero.

3 = RETURN without GOSUB
4 = Read past end of data
8 = Branch label not found
9 = Subscript out of range
11 = Division by zero
53 = OS Error: The system cannot find the file specified.
58 = OS Error: Cannot create a file when that file already exists.
55 = Error opening file
52 = Bad file handle
62 = Input past end of file

Example #1.
     [COLOR=Blue]on error goto[/COLOR] [errorHandling]
     [COLOR=Blue]print[/COLOR] [COLOR=Red]100[/COLOR]/[COLOR=Red]0[/COLOR]
     [COLOR=Blue]end[/COLOR]
[errorHandling]
     [COLOR=Blue]notice[/COLOR] [COLOR=Green]"Error "[/COLOR];[COLOR=Teal]Err[/COLOR];[COLOR=Green]" : "[/COLOR];[COLOR=Teal]Err$[/COLOR]
Output-> We will be noticed: "Notice 11 : Division by Zero".
This error occurs because we divided any number with zero, in this case we divided 100 with 0.

Example #2.
     [COLOR=Blue]on error goto[/COLOR] [errorHandler]
     [COLOR=Blue]for[/COLOR] [COLOR=Teal]x[/COLOR]=[COLOR=Red]1[/COLOR] to [COLOR=Red]10[/COLOR]
          [COLOR=Blue]print[/COLOR] [COLOR=Teal]x[/COLOR]
         [COLOR=Blue] if [/COLOR][COLOR=Teal]x[/COLOR]=[COLOR=Red]10[/COLOR] [COLOR=Blue]then[/COLOR] [finish]
     [COLOR=Blue]next[/COLOR]
     [COLOR=Blue]end[/COLOR]
[errorHandler]
     [COLOR=Blue]notice[/COLOR] [COLOR=Green]"Error "[/COLOR];[COLOR=Teal]Err[/COLOR];[COLOR=Green]" : "[/COLOR];[COLOR=Teal]Err$[/COLOR]
Output -> We will be noticed: "Error 8 : Branch label not found: [finish]"
This error occurs because we forget to put [finish] label. Try to put [finish] label like below and we will not get error notice displayed again.
     [COLOR=Blue]on error goto[/COLOR] [errorHandler]
     [COLOR=Blue]for[/COLOR] [COLOR=Teal]x[/COLOR]=[COLOR=Red]1[/COLOR] to [COLOR=Red]10[/COLOR]
          [COLOR=Blue]print[/COLOR] [COLOR=Teal]x[/COLOR]
         [COLOR=Blue] if [/COLOR][COLOR=Teal]x[/COLOR]=[COLOR=Red]10[/COLOR] [COLOR=Blue]then[/COLOR] [finish]
     [COLOR=Blue]next[/COLOR]
     [COLOR=Blue]end[/COLOR]
[finish]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"GREAT JOB!"[/COLOR]
     [COLOR=Blue]wait[/COLOR]
[errorHandler]
     [COLOR=Blue]notice[/COLOR] [COLOR=Green]"Error "[/COLOR];[COLOR=Teal]Err[/COLOR];[COLOR=Green]" : "[/COLOR];[COLOR=Teal]Err$[/COLOR]

Mini Quiz:

Do you can attempt to get error number 9 and 53? Why these are occurs and how we can accomplish them?
Answer:
Error #9.
We will be noticed: "Error 9 : Subscript out of range : 8, member$()."
This error occurs because we out of range from number of existing array. So, we must fill in seventh lines with range 0 up to 3, such as we change it by member$(2).
     [COLOR=Blue]dim [/COLOR]member$([COLOR=Red]3[/COLOR])
     member$([COLOR=Red]0)[/COLOR]=[COLOR=Green]"Jordan"[/COLOR]
     member$([COLOR=Red]1[/COLOR])=[COLOR=Green]"John"[/COLOR]
     member$([COLOR=Red]2[/COLOR])=[COLOR=Green]"James"[/COLOR]
     member$([COLOR=Red]3[/COLOR])=[COLOR=Green]"Jaan"[/COLOR]
     [COLOR=Blue]on error goto[/COLOR] [errorHandler]
     [COLOR=Blue]print [/COLOR]member$([COLOR=Red]8[/COLOR])
     [COLOR=Blue]end[/COLOR]
[errorHandler]
     [COLOR=Blue]notice[/COLOR] [COLOR=Green]"Error "[/COLOR];[COLOR=Teal]Err[/COLOR];[COLOR=Green]" : "[/COLOR];[COLOR=Teal]Err$[/COLOR]
Error #53.
We will be noticed: "Error 53 : OS Error: The system cannot find the file specified."
This error occurs because the file is not found (i assumed you don't have file with name arsenal.txt in your computer). So, we must create file with name arsenal.txt and save it same folder where we save this code (for default is Liberty BASIC folder) before we run this code.
     [COLOR=Blue]on error goto[/COLOR] [errorHandler]
    [COLOR=Blue] open[/COLOR] [COLOR=Green]"arsenal.txt"[/COLOR] [COLOR=Blue]for input as[/COLOR] #main
     [COLOR=Blue]close[/COLOR] #main
     [COLOR=Blue]end[/COLOR]
[errorHandler]
     [COLOR=Blue]notice[/COLOR] [COLOR=Green]"Error "[/COLOR];[COLOR=Teal]Err[/COLOR];[COLOR=Green]" : "[/COLOR];[COLOR=Teal]Err$[/COLOR]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users