From J Wiki
Jump to navigation Jump to search

>> <<   Back to: Vocabulary Thru to: Dictionary

Execute with error path
Error path
Error path only if not debugging
Catch throw. from called function

Valid only inside an explicit definition.

You put sentences into a try. structure when you want to recover if they fail. If there is an error in the block following try., the try. is aborted and control passes to the catch. . catcht. is a special case that picks up the special error case created by a throw. control word. As with other control structures, end. terminates the try. structure.

try. executes the try. block, which is the sentences following the try. up to the first catch./catcht./catchd.. If the try. block completes without error, execution continues after the end.. If an error is encountered, control passes to one of the catch blocks:

1. If the error is uncaught throw., in other words if throw. was executed in a called function and has not already been intercepted, then:

  • if there is a catcht. block in the current try.structure, control passes to the first sentence of the catcht. block and the error is reset;
  • otherwise, the current definition is terminated with the uncaught throw. error

2. For other errors, if there is a catch. block, control passes to the first sentence of the catch. block and the error is reset.
3. However, if there is a catchd. word and debugging has been turned on, execution is suspended at the point of error.
4. If there is catchd. word and no catch., the catchd. also starts the catch. that handles the error
Debugging is turned off (including stops) during the execution of the try. block, except when a catchd. block is present.

Normal Uses

Two use cases are envisioned:

1. To wrap an application with an error handler that catches errors for logging or presenting to the user, write

 ''call the application''
 emsg=.13!:12''  NB. save the error message for the error
 eno =. 13!:11''  NB. also the error number

Because you used catchd., you can turn on debugging to trap on the error.

2. If execution errors are part of normal operation, write

line =. ''read a command from the user''
 cmdresult =. ". line  NB. Execute the user's line - it may fail
 cmdresult =. ''default result if there was an error''

Because you used catch., debugging will be turned off before the user's line is executed, even if you are debugging the rest of your application.