Google Search - Blog...........

ABAP - Use Of Method Raising Exceptions - OOPS Concept.

Methods can raise exceptions like function modules which can be handled after calling the method, depending on various sy-subrc values. This program will demonstrate that.
------------------------------------------------------------------------------------------
report ysubdel1 message-id 00.

class c1 definition .
public section.
methods : m1 importing num1 type i
exporting num2 type i
exceptions e1.
endclass.

class c1 implementation.
method : m1.
if num1 lt 5 .
message i398(00) with 'Should be >=5' raising e1.
else .
num2 = num1 * 5 .
endif.
endmethod.
endclass.

parameters : p_no type i .

start-of-selection.
data : obj1 type ref to c1 .
create object obj1.
call method obj1->m1 exporting num1 = p_no
importing num2 = p_no
exceptions e1 = 1.

IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
write:/5 p_no .
ENDIF.

Output:

The program provides the user a selection-screen where the user enters a numeric value. If the user entry is <5,>=5’. Else, five times of the value entered is displayed by the program on execution.

No comments:

Post a Comment