Skip to content

CX_SY_ZERODIVIDE

Raised when an integer or packed arithmetic division by zero is attempted.

Purpose

CX_SY_ZERODIVIDE is thrown by the /, DIV, and MOD operators when the divisor is zero and the operands are integer (TYPE I, INT8) or packed (TYPE P) types.

Inheritance chain: CX_SY_ZERODIVIDE → CX_SY_ARITHMETIC → CX_DYNAMIC_CHECK → CX_ROOT

Float division behaves differently

TYPE F (floating point) division by zero does not raise this exception — it produces +Infinity. Only integer and packed types raise CX_SY_ZERODIVIDE. If not caught, the runtime dumps with COMPUTE_INT_ZERODIVIDE.

How to catch

REPORT z_zerodiv_guard.

START-OF-SELECTION.

  DATA: lv_total TYPE i VALUE 100,
        lv_count TYPE i VALUE 0,
        lv_avg   TYPE i.

  IF lv_count > 0.
    lv_avg = lv_total / lv_count.
  ELSE.
    lv_avg = 0.
    WRITE: / 'Count is zero — average set to 0'.
  ENDIF.

  WRITE: / 'Average:', lv_avg.
REPORT z_zerodiv_catch.

START-OF-SELECTION.

  DATA: lv_total TYPE i VALUE 100,
        lv_count TYPE i VALUE 0,
        lv_avg   TYPE i.

  TRY.
      lv_avg = lv_total / lv_count.
    CATCH cx_sy_zerodivide INTO DATA(lx).
      lv_avg = 0.
      WRITE: / 'Division by zero caught:', lx->get_text( ).
  ENDTRY.

  WRITE: / 'Average:', lv_avg.
REPORT z_zerodiv_cond.

START-OF-SELECTION.

  DATA: lv_actual TYPE p DECIMALS 1 VALUE '75',
        lv_total  TYPE p DECIMALS 1 VALUE '0',
        lv_pct    TYPE p DECIMALS 1.

  " Guard total > 0 before dividing — COND #( ) infers type from lv_pct
  lv_pct = COND #(
    WHEN lv_total > 0
    THEN lv_actual * 100 / lv_total
    ELSE 0 ).

  WRITE: / 'Percentage:', lv_pct, '%'.

Key attributes

Attribute Type Description
Inherited: get_text( ) string Short message: "Division by zero"
Inherited: get_longtext( ) string Extended description

See also

Comments