|
|||||||||
|
|
|||||||||
| Related Syntax | Related Concepts | ||||||||
| control structure | using catch | ||||||||
Syntax
using catch
block
You can use using catch to clarify the intention of your code when using catch and throw. Because a catch block comes at the end of the scope in which it occurs, it is not always clear as you read the code that the use of catch is an important part of the program logic at this point. using catch allows you to move the catch to the top of a block. Thus you can replace the following code:
find "{"
submit #current-input
catch close-brace
with the following:
find "{"
using catch close-brace
submit #current-input
There is no functional difference between the two samples, but the second gives a clearer indication of the logical relationship between the submit and the catch.
Note that unlike catch, using catch does not begin a catch block. The code block prefixed by using catch is a normal code block that will be executed according to the normal flow control of the program. The using catch prefix simply means that the named catch is active in that code block. If a throw to that catch occurs, execution will resume outside the code block.
With using catch, there is no catch block and no parameters are passed to the catch. You can, however, have always blocks in a code block prefixed by using catch.
using catch is appropriate for those situations in which you are using catch and throw purely for flow control and do not want to execute any special code when the catch occurs. Possible uses include the following:
Exit from multiple levels of loops
You can use using catch to allow easy exit from multiple levels of looping. In the following code, execution will resume outside the outermost loop following the throw to "long-exit":
declare catch long-exit
;...
using catch long-exit
repeat
; ...
repeat
; ...
throw long-exit
; ...
again
; ...
again
Exiting from a do
You can use using catch to allow you to exit from a do block. In the following code, execution will resume after the done:
using catch do-exit
do
; ...
do when ...
; ...
throw do-exit
done
; ...
do
Ending a submit
You can use using catch to end the processing of a submitted data source when some condition occurs. In the following code, processing of the source will cease after the first control-z character is matched:
process
using catch ctrl-z
submit file #args [1]
find "%26#" ; control-z
throw ctrl-z
|
Related Syntax catch declare catch |
Related Concepts Catch and throw |
| ---- |