SAP Tutorials Blog


 


DATA(day_or_night) = COND #( WHEN cl_abap_context_info=>get_system_time( ) BETWEEN '050000' AND '220000'

                             THEN `day`

                             ELSE `night` ).


"A constructor expression as above instead of, for example, an IF statement as follows.

IF cl_abap_context_info=>get_system_time( ) BETWEEN '050000' AND '220000'.

    day_or_night = `day`.

ELSE.

    day_or_night = `night`.

ENDIF.


"Multiple logical expressions initiated by WHEN

"Also LET expressions are possible. See more details further down.

DATA(time_of_day) = COND #( LET time = cl_abap_context_info=>get_system_time( ) IN

                            WHEN time BETWEEN '050001' AND '120000' THEN |Good morning, it's { time TIME = ISO }.|

                            WHEN time BETWEEN '120001' AND '180000' THEN |Good afternoon, it's { time TIME = ISO }.|

                            WHEN time BETWEEN '180001' AND '220000' THEN |Good evening, it's { time TIME = ISO }.|

                            ELSE |Good night, it's { time TIME = ISO }.| ).


"THROW addition to raise an exception (working like RAISE EXCEPTION TYPE statements)

"by specifying an exception class

"Note: It is possible to ...

"- specify the THROW addition also after THEN.

"- make exceptions resumable using the RESUMABLE addition.

DATA(num1) = 0.

DATA(num2) = 0.

TRY.

    "The example raises the exception because both operands have the value 0.

    DATA(div) = COND decfloat34( WHEN num1 <> 0 AND num2 <> 0 THEN num1 / num2

                                 WHEN num1 = 0  AND num2 <> 0 THEN num1 / num2

                                 ELSE THROW cx_sy_zerodivide( ) ).

   CATCH cx_sy_zerodivide.

    DATA(two_zeros) = `Zero division`.

ENDTRY.


"Excursion for the example above: The following statement does not result in an 

"error in ABAP (zero division 'allowed' if the first operand has also the value 0).

div = 0 / 0.


"THROW SHORTDUMP addition to raise a runtime error (working like RAISE SHORTDUMP

"TYPE statements) by specifying an exception class; a message can be also passed,

"and input parameters can be filled

div = COND decfloat34( WHEN num1 <> 0 AND num2 <> 0 THEN num1 / num2

                       WHEN num1 = 0  AND num2 <> 0 THEN num1 / num2

                       ELSE THROW SHORTDUMP cx_sy_zerodivide( ) ).


Serkan AKKAVAK

Computer Engineer BSc

SAP Department Manager

Contact : serkurumsal@yandex.com