Sheerpower®
A Guide to the Sheerpower Language


Previous Contents Index

EXAMPLE:

Example 13-3 WHEN EXCEPTION USE

  input 'Enter total sales amount': tsales 
  input 'Enter number of sales': nsales 
  when exception use fix_average 
    average = tsales/nsales 
  end when
  print 'The average is:'; average 
 
  handler fix_average 
    average = 0 
    continue
  end handler
  end
 
        
Enter total sales amount? 25.00 
Enter number of sales? 0 
The average is: 0 

PURPOSE:

WHEN EXCEPTION USE catches runtime exceptions and resolves them within a program when the same handler is needed for several protected blocks, or when all the handlers are to be in one place in a program.

DESCRIPTION:

WHEN EXCEPTION USE begins the protected block of code and specifies the HANDLER routine to use. handl_name is the name of the handler routine. The handler name must meet the specifications for variable names. The protected block is everything between the WHEN EXCEPTION USE and the END WHEN statements. If an exception occurs in this block of code, SheerPower calls the handler specified. If the handler does not exist, an error is generated.

HANDLER begins the handler routine. Everything between the HANDLER and the END HANDLER constitutes the handler routine. END HANDLER returns control to the statement following the END WHEN. When the handler is called, this block of code is executed. In the example above, SheerPower would normally return an exception when it tried to divide 25.00 by 0. The exception handler FIX_AVERAGE intercepts this exception and sets AVERAGE equal to 0.

The handler routine can occur before or after the protected block. For example:

Example 13-4 HANDLER/END HANDLER with WHEN EXCEPTION USE

  handler fix_average 
    average = 0               //<--- handler routine 
    continue
  end handler
 
  input 'Enter total sales amount': tsales 
  input 'Enter number of sales': nsales 
  when exception use fix_average 
    average = tsales/nsales 
  end when
  print 'The average is'; average 
  end

One of the advantages of WHEN EXCEPTION USE is that the same handler routine can be called by any number of WHEN EXCEPTION USE statements. For example:

Example 13-5 HANDLER/END HANDLER with WHEN EXCEPTION USE

  when exception use numbers 
    input 'How old are you': age      //<--- first protected block 
  end when
 
  input 'Your name, please': name$ 
 
  when exception use numbers 
    input 'Your birthdate': birth     //<--- second protected block 
  end when
 
  print name$; ' was born on'; birth 
 
  handler numbers 
    print 'Enter numbers only, please.'    //<--- handler routine 
    retry
  end handler
  end
  
 
How old are you? 31       <---- type in your age 
Your name, please? Sunny  <---- type in your name 
Your birthdate? 10181969  <---- type in your birthdate (no spaces) 

13.4 HANDLER Routine Actions

13.4.1 RETRY

FORMAT:


        USE 
          --- 
          ---  RETRY 
          --- 
        END WHEN 
 
           or 
        
        HANDLER handl_name 
          --- 
          ---  RETRY 
          --- 
        END HANDLER 

EXAMPLE:

Example 13-6 RETRY statement in HANDLER routine

  input 'Your name, please': name$ 
  when exception in
    input 'How old are you': age 
  use
    print 'Not a valid age' 
    retry
  end when
  print
  print name$; ' is'; age 
  end
 
        
Your name, please? Tester 
How old are you? 3x 
Not a valid age 
How old are you? 35 
 
Tester is 35 

PURPOSE:

RETRY is used after an exception to re-execute the statement that generated the exception.

DESCRIPTION:

RETRY can be used only in a HANDLER routine. RETRY causes SheerPower to leave the handler and re-execute the statement that generated an exception.

13.4.2 CONTINUE

FORMAT:


        USE 
          --- 
          ---  CONTINUE 
          --- 
        END WHEN 
 
           or 
 
        HANDLER handl_name 
          --- 
          ---  CONTINUE 
          --- 
        END HANDLER 

EXAMPLE:

Example 13-7 CONTINUE statement in HANDLER routine

  input 'Enter total sales amount': tsales 
  input 'Enter number of sales': nsales 
  when exception use fix_average 
    average = tsales / nsales 
  end when
  print 'The average is:'; average 
 
  handler fix_average 
    average = 0 
    continue
  end handler
  end
 
        
Enter total sales amount? 18.00 
Enter number of sales? 0 
The average is: 0 

PURPOSE

CONTINUE is used to continue normal program execution at the statement following the one that generated the exception.

DESCRIPTION:

CONTINUE causes SheerPower to exit the exception handler and continue program execution at the first statement following the statement which generated the exception. CONTINUE can only be used in a HANDLER routine.

13.4.3 RESUME

FORMAT:


        USE 
          --- 
          ---  RESUME target 
          --- 
        END WHEN 
 
           or 
 
        HANDLER handl_name 
          --- 
          ---  RESUME target 
          --- 
        END HANDLER 

EXAMPLE:

Example 13-8 RESUME statement in HANDLER routine

  input 'Enter total sales amount': tsales 
  input 'Enter number of sales': nsales 
  when exception use fix_average 
    average = tsales / nsales 
  end when
  print 'The average is:'; average 
 
  handler fix_average 
    average = 0 
    print 'Invalid numbers.  Try again.' 
    resume 1 
  end handler
  end
 
        
Enter total sales amount? 75.00 
Enter number of sales? 0 
Invalid numbers.  Try again. 
Enter total sales amount? 75.00 
Enter number of sales? 3 
The average is: 25 

PURPOSE:

RESUME is used to resume normal program execution at the routine name or line number that you specify.

DESCRIPTION:

RESUME causes SheerPower to exit the exception handler and resume program execution at the routine name specified.

RESUME can only be used in a handler routine.

13.4.4 EXIT HANDLER

FORMAT:


        USE 
          --- 
          ---  EXIT HANDLER 
          --- 
        END WHEN 
 
           or 
 
        HANDLER handl_name 
          --- 
          ---  EXIT HANDLER 
          --- 
        END HANDLER 

EXAMPLE:

Example 13-9 EXIT HANDLER statement in HANDLER routine

  when exception use mistake 
    input 'Enter your age': age 
  end when
  print 'You are'; age; 'years old' 
 
  handler mistake 
    print 'Oops...' 
    delay 2 
    exit handler
  end handler
  end
 
        
Enter your age? 3x 
Oops... 
Non-numeric input when number expected at 10.1 
Enter your age? 35 
You are 35 years old 

PURPOSE:

EXIT HANDLER is used to exit a handler routine and pass the exception up to the next level of exception handling.

DESCRIPTION:

EXIT HANDLER exits the current HANDLER routine. If the current handler is nested within another handler, SheerPower jumps to the outside handler and executes the code for that handler. If there is no other exception handler, control returns to the SheerPower system. SheerPower prints the exception message and takes whatever action it normally would.

EXIT HANDLER can only be used within an exception handler.


Previous Next Contents Index