Exceptions in abap

 Exceptions: - Exception is nothing but run time error. We handle these exceptions by using TRY & CATCH blocks.

Note: - CX_ROOT is used to store the error.


Code

REPORT ZAJAY_EXCEPTION_EXMP1.
class lcl_excep DEFINITION.
  PUBLIC SECTION.
  METHODS l_m_excep IMPORTING imp_var1 type imp_var2 TYPE EXPORTING e_sum type i.

  ENDCLASS.

CLASS lcl_excep IMPLEMENTATION.
  METHOD l_m_excep.
    TRY.
      e_sum imp_var1 / imp_var2.


    CATCH CX_ROOT.
      MESSAGE 'CAN NOT DEVISIBLE BY ZERO' TYPE 'I'.

    ENDTRY.

    ENDMETHOD.
  ENDCLASS.

  START-OF-SELECTION.
  PARAMETERSp1 type i,
              p2 TYPE i.
  data sum type i.
  data obj type REF TO lcl_excep.
  create OBJECT obj.
  obj->l_m_excep(
    EXPORTING
      imp_var1 p1
      imp_var2 p2
    IMPORTING
      e_sum    sum
  ).
  
IF SUM <> 0.

    WRITE'divisible of two numbers is 'sum.

  ENDIF.

Notes

    * If you don't use exception concept, if any error comes then raise a dump and program terminates.

    *  If you are using exception concept, if any error comes it catches the exception and raise message and program execute successfully.



Comments

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).