| Previous | Contents | Index |
Exception handling routines intercept runtime exceptions and execute a block of code which handles them. If there is no exception handler, SheerPower returns an exception message specifying what the exception was and where it occurred. SheerPower stops program execution or tries the offending statement again.
There are two types of exception handlers: attached handlers and detached handlers.
For an attached handler, a WHEN EXCEPTION IN statement is used. WHEN EXCEPTION IN puts the handler (the block of code to execute) right after the block of code it protects.
For a detached handler, the statement WHEN EXCEPTION USE is used. When an exception occurs in the protected block, WHEN EXCEPTION USE calls a handler routine located in some other part of the program. The same handler routine can be used by any number of WHEN EXCEPTION USE statements and can be placed anywhere in a program.
This section explains exception handlers. It also describes the handling statements--RETRY, CONTINUE and EXIT HANDLER.
The following functions are also related to exception handling:
For a full description of these functions, see Section 6.8, Debugging and Exception Handling Functions.
CAUSE EXCEPTION exception_number
|
| Example 13-1 CAUSE EXCEPTION |
|---|
do
input 'Select a number between 1 and 10': no
if no < 1 or no > 10 then cause exception 1001
repeat do
end do
end
Select a number between 1 and 10? 8
Select a number between 1 and 10? 99
Illegal number at 10.2
|
CAUSE EXCEPTION is used to generate an exception under specific conditions.
CAUSE EXCEPTION causes the specified exception to occur. exception_number can be any integer expression. When SheerPower executes the CAUSE EXCEPTION statement, it generates the exception specified. See Section C.2, Exceptions for a list of exception messages.
WHEN EXCEPTION IN
---
--- protected block
---
USE
---
--- handler
---
END WHEN
|
| Example 13-2 WHEN EXCEPTION IN/USE/END WHEN |
|---|
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
|
WHEN EXCEPTION IN is used to catch runtime exceptions and resolve them within a program when the code handling the exception is to be next to the code being protected.
WHEN EXCEPTION IN begins the protected block of code. Everything between the WHEN EXCEPTION IN and USE statements constitute the section of code protected by the handler---the protected block.
USE begins the handler routine. Everything between the USE and the END WHEN statements constitutes the handler. If an exception occurs in the protected block, the handler code is executed. END WHEN ends the routine.
WHEN EXCEPTION USE handl_name
---
--- protected block
---
END WHEN
.
.
.
HANDLER handl_name
---
--- handler
---
END HANDLER
|
| 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
|
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.
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
USE
---
--- RETRY
---
END WHEN
or
HANDLER handl_name
---
--- RETRY
---
END HANDLER
|
| 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
|
RETRY is used after an exception to re-execute the statement that generated the exception.
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.
USE
---
--- CONTINUE
---
END WHEN
or
HANDLER handl_name
---
--- CONTINUE
---
END HANDLER
|
| 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
|
| Previous | Next | Contents | Index |