ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Conditional Expressions
COND, SWITCH - result
Other versions:
7.31 | 7.40 | 7.54
Syntax
... operand
| { THROW [RESUMABLE|SHORTDUMP] cx_class( [
message]
[p1 = a1 p2 = a2 ...] ) } ...
Alternatives
2. ... THROW [RESUMABLE|SHORTDUMP] cx_class( [message] [p1 = a1 p2 = a2 ...] )
Effect
When specified after THEN
and ELSE
in the
conditional expressions
COND
and
SWITCH
, these strings determine the result of the expression if the appropriate branch is selected.
Alternative 1
... operand
Effect
If an operand operand
is specified, its value is converted to the data type
type
if necessary and returned as the result of the conditional expression. operand
is a
general expression position with the following restrictions:
- If
operand
is specified as a string expression,type
must be character-like.
- If
operand
is specified as a bit expression,type
must be byte-like.
Note
When an operand is specified, the condition operators COND
and SWITCH
create a temporary data object whose data type is determined by the specified type and whose content
is determined by the selected operand. This data object is used as the operand of a statement and then
deleted. It is deleted either when the current statement is closed or when a relational expression is evaluated after the truth value is determined.
Example
Specifies the operands sum
and sum + 1
. The number of times a random number is less than half of its maximum value is counted.
DATA(rnd) = cl_abap_random_int=>create(
seed = CONV i( sy-uzeit ) min = 1 max = 101 ).
DATA(sum) = 0.
DO 100 TIMES.
sum = COND i( WHEN rnd->get_next( ) <= 50 THEN sum + 1
ELSE sum ).
ENDDO.
cl_demo_output=>display( sum ).
Alternative 2
... THROW [RESUMABLE|SHORTDUMP] cx_class( [message] [p1 = a1 p2 = a2 ...] )
Effect
If specified, THROW
raises either an exception or a runtime error.
- If the addition
SHORTDUMP
is not specified,THROW
functions like the statementRAISE EXCEPTION TYPE
and raises an exception of the exception classcx_class
. The following applies here:
- The addition
RESUMABLE
can be used to make the exception resumable.
- A message can be passed to the exception object using the addition
message
.
- The input parameters
p1
,p2
, ... of the instance constructor can be filled with actual parametersa1
,a2
.
- If the addition
SHORTDUMP
is specified,THROW
functions like the statementRAISE SHORTDUMP TYPE
and raises a runtime error with the exception classcx_class
. In both cases, a message can be passed and the input parameters can be filled.
Notes
- If the exception class is specified after
THROW
, the parentheses must always be specified, even if no messages or actual parameters are passed.EXPORTING
cannot be specified, nor does it need to be.
- Like the statement RAISE
EXCEPTION,
THROW
specified without the additionSHORTDUMP
cannot be used in a method or function module in whose interface non-class-based exceptions are declared. Also, the statement does not permit simultaneous use of the statementCATCHSYSTEM-EXCEPTIONS
for the obsolete handling of catchable runtime errors, and the statementsRAISE
orMESSAGE RAISING
to raise non-class-based exceptions in the current processing block.
Example
Raises an exception of the class CX_DEMO_DYN_T100 that includes the interface IF_T100_DYN_MSG.
DATA cflag TYPE abap_bool.
...
DATA(iflag) = COND i( WHEN cflag = abap_true THEN 1
WHEN cflag = abap_false THEN 0
ELSE THROW cx_demo_dyn_t100(
MESSAGE e888(sabapdemos)
WITH 'Illegal value!' '' '' '' ) ).
Example
Raises a runtime error with an exception object of the class CX_DEMO_DYN_T100, which includes the interface IF_T100_DYN_MSG.
DATA(time) = COND t( WHEN sy-timlo BETWEEN '090000' AND '170000'
THEN sy-timlo
ELSE
THROW SHORTDUMP cx_demo_dyn_t100(
MESSAGE e888(sabapdemos)
WITH `I` `don't` `work` `now!` ) ).