Exceptions, References, Directives (::routine, ::requires)
::REQUIRES directives are carried out
::ROUTINE, ::CLASS, … ) are carried outConditions 1
Conditions
| SYNTAX | Statement not syntactically correct |
| FAILURE | Error in external program |
| ERROR | Error in external program, not trapped with "FAILURE" or "ANY" |
| HALT | Ctl-C (Ctl-Break): user aborts program |
| NOVALUE | Do not allow non-initialised variables |
| USER usercondition | User-defined condition named "usercondition" |
| LOSTDIGITS | Needs more digits than NUMERIC DIGITS |
| NOMETHOD, NOSTRING, NOTREADY (later ... ) | |
| ANY | Intercepts (traps) all conditions |
Conditions 2
Conditions 3
Activate SYNTAX condition handling ("SIGNAL ON SYNTAX"), jump to label "ANY:"
/* */
SIGNAL ON SYNTAX NAME ANY /* target name "ANY" given */
SAY Nix /* Variable not initialized! */
EXIT 0
ANY: /* target for any exception */
exc_rc = RC /* save return code */
exc_sigl = SIGL /* save line number */
exc_type = CONDITION("C") /* get exception type */
CALL say2stderr "REXX 'RC':" exc_rc
CALL say2stderr " type:" exc_type
CALL say2stderr
CALL say2stderr " in line:" exc_sigl
CALL say2stderr " " SOURCELINE(exc_sigl)
EXIT -1 /* indicate error */
SAY2STDERR: /* write to STDERR: */
CALL LINEOUT "STDERR", ARG(1)
RETURN
Output:
NIXConditions 4
/* */
SIGNAL ON NOVALUE NAME ANY
SAY Nix /* Variable not initialized! */
EXIT 0
ANY: /* target for any exception */
exc_rc = RC /* save return code */
exc_sigl = SIGL /* save line number */
exc_type = CONDITION("C") /* get exception type */
CALL say2stderr "REXX 'RC':" exc_rc
CALL say2stderr " type:" exc_type
CALL say2stderr
CALL say2stderr " in line:" exc_sigl
CALL say2stderr " " SOURCELINE(exc_sigl)
EXIT -1 /* indicate error */
SAY2STDERR: /* write to STDERR: */
CALL LINEOUT "STDERR", ARG(1)
RETURN
Output:
C:\temp>rexx test.rex
REXX 'RC': RC
type: NOVALUE
in line: 3
SAY Nix /* Variable not initialized! */
Conditions 5
/* */
SIGNAL ON NOVALUE NAME ANY
SAY Nix /* Variable not initialized! */
EXIT 0
ANY: /* target for any exception */
exc_rc = RC /* save return code */
exc_sigl = SIGL /* save line number */
exc_type = CONDITION("C") /* get exception type */
CALL say2stderr "REXX 'RC':" exc_rc
CALL say2stderr " type:" exc_type
CALL say2stderr
CALL say2stderr " in line:" exc_sigl
CALL say2stderr " " SOURCELINE(exc_sigl)
EXIT -1 /* indicate error */
SAY2STDERR: /* write to STDERR: */
CALL LINEOUT "STDERR", ARG(1)
RETURN
Running program with redirecting error output to a file:
C:\temp>rexx test.rex 2>myerrors.txt
REXX 'RC': RC
type: NOVALUE
in line: 3
SAY Nix /* Variable not initialized! */Excursus 1
0)": default input via the keyboard1)": default output to the screen (window)2): default output to the screen (window)PATH … determines the directories and the order for seeking programsJAVA_HOME … determines the Java directory to useCLASSPATH … determines the directories and the order for seeking Java classesExcursus 2
Redirect input from the keyboard to the file "myinput.txt"
Redirect output from the screen to the file "myoutput.txt" (will delete file if it exists)
Redirect error output from the screen to the file "myerrors.txt" (will delete file if it exists)
Excursus 3
Redirecting standard files when running a program in a process
rexx myprogram.rex 0<myinput.txt 1>myoutput.txt 2>myerrors.txt
rexx myprogram.rex <myinput.txt >myoutput.txt 2>myerrors.txtRedirect output to a file and also redirect error output to the same file
Redirecting and appending to stdout and to stderr
Excursus 4
Redirect stdout of one program to stdin of the next program
Piping operator: vertical bar ("|")
Example
One can redirect and pipe to/from multiple programs
Excursus 5
Unix null device, a file named
/dev/null
Windows null device, a pseudo file named
nul
Show errors only (discard stdout output)
rexx myprogram.rex >nul (Windows)
rexx myprogram.rex >/dev/null (Unix)
Show output only (discard stderr output, i.e. do not show error messages)
rexx myprogram.rex 2>nul (Windows)
rexx myprogram.rex 2>/dev/null (Unix)
Excursus 6
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Unix, Windows):
rexx myprogram.rex < myinput.txt
Max
und
Moritz
konnt
der
Lehrer ...Output:
Max
und
Moritz
konnt
der
Lehrer ...
Excursus 7
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Windows):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex >nul
Command (Unix):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex >/dev/null
Max
und
Moritz
konnt
der
Lehrer ...Output:
Max
und
konnt
Excursus 8
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Windows):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>nul
Command (Unix):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>/dev/null
Max
und
Moritz
konnt
der
Lehrer ...Output:
Moritz
der
Lehrer ...
Excursus 9
/* myprogram.rex: write input lines (stdin) to output (stdout) */
do until value="" -- if empty input, leave
parse pull value -- read line from stdin, assign to variable "value"
say value -- write to stdout
end
Command (Windows):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>nul | sort
Command (Unix):
rexx myprogram.rex < myinput.txt | rexx myfilter.rex 2>/dev/null | sort -f
Max
und
Moritz
konnt
der
Lehrer ...Output:
der
Lehrer ...
Moritz
Conditions 6
/* */
SIGNAL ON NOVALUE NAME ANY
SAY Nix /* Variable not initialized! */
EXIT 0
ANY: /* target for any exception */
exc_rc = RC /* save return code */
exc_sigl = SIGL /* save line number */
exc_type = CONDITION("C") /* get exception type */
CALL say2stderr "REXX 'RC':" exc_rc
CALL say2stderr " type:" exc_type
CALL say2stderr
CALL say2stderr " in line:" exc_sigl
CALL say2stderr " " SOURCELINE(exc_sigl)
EXIT -1 /* indicate error */
SAY2STDERR: /* write to STDERR: */
CALL LINEOUT "STDERR", ARG(1)
RETURN
Output:
C:\temp>rexx test.rex 2>myerrors.txt
REXX 'RC': RC
type: NOVALUE
in line: 3
SAY Nix /* Variable not initialized! */Conditions 7
Usually, the Rexx-Interpreter raises conditions …
… but you can also do it! :)
RAISE statement
RAISE PROPAGATE
Can only be given during condition (exception) handling
Re-creates the same exception in the caller, which allows the caller to also intercept it!
Output:
hallo
3 *-* RAISE SYNTAX 9.1 /* Pretend syntax error # 9.1 */
Error 9 running C:\temp\demoRaiseSyntax.rex line 3:
Unexpected WHEN or OTHERWISE
Error 9.1: WHEN has no corresponding SELECTConditions 8
/**/
SIGNAL ON SYNTAX /* no label, hence "SYNTAX" */
SAY "hallo"
RAISE SYNTAX 9.1 /* Pretend syntax error # 9.1 */
EXIT 0
SYNTAX: /* target for any exception */
SAY "In SYNTAX-exception handling code."
EXIT -1
Output:
hallo
In SYNTAX-exception handling code.
Conditions 9
/**/
SIGNAL ON ANY /* no label, hence "ANY" */
SAY "hallo"
RAISE SYNTAX 9.1 /* Pretend syntax error # 9.1 */
EXIT 0
ANY: /* target for any exception */
exc_rc = RC /* save return code */
exc_sigl = SIGL /* save line number */
exc_type = CONDITION("C") /* get exception type */
CALL say2stderr "REXX 'RC':" exc_rc
CALL say2stderr " type:" exc_type
CALL say2stderr
CALL say2stderr " in line:" exc_sigl
CALL say2stderr " " SOURCELINE(exc_sigl)
EXIT -1 /* indicate error */
SAY2STDERR: /* write to STDERR: */
CALL LINEOUT "STDERR", ARG(1)
RETURN
Output:
REXX 'RC': 9
type: SYNTAX
in line: 4
RAISE SYNTAX 9.1 /* Pretend syntax error # 9.1 */
Routine 1
Routine 2
/**/
SAY pp("hello")
CALL oha /* routine is called */
SAY pp("hello")
EXIT 0
pp : RETURN "<<<" || ARG(1) || ">>>"
:: ROUTINE oha PUBLIC
SAY pp("holla")
EXIT 0
pp : RETURN "[" || ARG(1) || "]"
Output:
<<<hello>>>
[holla]
<<<hello>>>
Routine 3
Routine directives are treated as if they were proper programs
/**/
SIGNAL ON USER TOO_SMALL /* intercept a user exception */
CALL checkAge 10
CALL checkAge 3
CALL checkAge 7
EXIT 0
TOO_SMALL: /* dealing with the user exception */
SAY "// caught exception 'TOO_SMALL' \\"
EXIT -1
::ROUTINE checkAge
PARSE ARG age
SAY "--> age:" age
IF age < 6 THEN RAISE USER too_small
ELSE SAY "--> checked o.k."
EXIT 0
Output:
Routine 4
/**/
CALL ON USER TOO_SMALL /* intercept a user exception */
CALL checkAge 10
CALL checkAge 3
CALL checkAge 7
EXIT 0
TOO_SMALL: /* dealing with the user exception */
SAY "// caught exception 'TOO_SMALL' \\"
RETURN
::ROUTINE checkAge
PARSE ARG age
SAY "--> age:" age
IF age < 6 THEN RAISE USER too_small
ELSE SAY "--> checked o.k."
EXIT 0
Output:
Routine 5
CALL ON ANY /* intercept anything that is not caught explicitly */
CALL ON USER TOO_SMALL /* intercept a user exception */
CALL ON USER too_big /* intercept a user exception */
CALL checkAge 10
CALL checkAge 3
CALL checkAge 7 /* <- this is line # 6 */
EXIT 0
ANY : SAY "in line:" SIGL "exception:" CONDITION("C"); RETURN
Too_small: SAY "// caught exception 'TOO_SMALL' \\"; RETURN
TOO_BIG : SAY "// caught exception 'TOO_BIG' \\"; RETURN
::ROUTINE checkAge
PARSE ARG age
SAY '--> age:' age
IF age < 6 THEN RAISE USER too_small
ELSE IF age > 9 THEN RAISE USER too_big
ELSE SAY '--> checked o.k.'
RAISE USER something_raised
EXIT 0
Output:
Requires 1
::Requires "rexxprogram.rex"
::Routine, ::Class, ::Method, …)Output:
Requires 2
WU
Prof. Mag. Dr. Rony G. Flatscher
Welthandelsplatz 1
1020 Wien/Vienna
Austria/Europe
www: www.wu.ac.at