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 i imp_var2 TYPE i 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.
PARAMETERS: p1 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.
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
Post a Comment