Respond to this messageReturn to Index
Original Message
  • Maybe this sample can help:
    • Solitaire (Login Solitaire1)
      S
      Posted Sep 30, 2011 4:51 AM

      The first sample has all the code in the main program.  The second sample calls a subprocedure.

       

      ===================================================================================

       

      CLS  'use of ON ERROR GOTO to avoid run-time error
      A = 10: B = 0
      ON ERROR GOTO correct1
      C = A / B           'this line will be skipped
      PRINT "C ="; C
      A = 10: B = 0
      ON ERROR GOTO correct2
      C = A / B           'this line will be repeated
      PRINT "C ="; C
      A = 10: B = 0
      ON ERROR GOTO correct3
      C = A / B           'uses a flag to signal error
      IF misflag = 1 THEN
             C = A / B
             PRINT "C ="; C
      END IF
      PRINT : PRINT "This is the last line of the program."
      END

       

      correct1:
      PRINT : PRINT "Division by zero error.  B = 2 corrects error."
      B = 2
      PRINT "RESUME NEXT will skip line that caused error."
      PRINT "C = A / B  never gets executed."
      RESUME NEXT

       

      correct2:
      PRINT : PRINT "Division by zero error.  B = 2 corrects error."
      B = 2
      PRINT "RESUME will repeat line that caused error."
      PRINT "C = A / B  does get executed."
      RESUME

       

      correct3:
      PRINT : PRINT "Division by zero error.  B = 2 corrects error."
      B = 2
      PRINT "RESUME - using a flag to signal error."
      PRINT "C = A / B is executed in 'IF misflag = 1' block."
      misflag = 1
      RESUME

       

      ======================================================================================

       

      DECLARE SUB Mistake (A, B, C)

      COMMON SHARED misflag

      CLS  'use of ON ERROR GOTO in a subprocedure

      PRINT "This is the first line of the main program."

      PRINT "The rest of the code is in Sub Mistake."

      PRINT "All the errorhandlers must be in the main program."

      PRINT "All label names in entire program must be different."

      CALL Mistake(A, B, C)

      PRINT "This is the last line of the main program."

      END

       

      correct1:

      PRINT : PRINT "Division by zero error.  B = 2 corrects error."

      B = 2

      PRINT "RESUME NEXT will skip line that caused error."

      PRINT "C = A / B  never gets executed."

      RESUME NEXT

       

      correct2:

      PRINT : PRINT "Division by zero error.  B = 2 corrects error."

      B = 2

      PRINT "RESUME will repeat line that caused error."

      PRINT "C = A / B  does get executed."

      RESUME

       

      correct3:

      PRINT : PRINT "Division by zero error.  B = 2 corrects error."

      B = 2

      PRINT "Uses a flag to signal an error in the subprocedure."

      PRINT "Flag should be declared globally."

      PRINT "C = A / B is executed in 'IF misflag = 0' block."

      misflag = 1

      RESUME NEXT

       

      SUB Mistake (A, B, C)

      A = 10: B = 0

      ON ERROR GOTO correct1

      C = A / B           'this line will be skipped

      PRINT "C ="; C

      A = 10: B = 0

      ON ERROR GOTO correct2

      C = A / B           'this line will be repeated

      PRINT "C ="; C

      A = 10: B = 0

      ON ERROR GOTO correct3

      C = A / B           '

      IF misflag = 1 THEN     'flag used to signal error

             misflag = 0         'resets flag

             C = A / B           'corrects error

             PRINT "C ="; C

      END IF

      PRINT "This is the last line of the subprocedure."

      END SUB

    Your Name
    Your Email
    (Optional)
    Message Title
    Message Text
    Options Also send responses to my email address
          


     Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement