Thursday, 13 June 2019

Creating Macros in ABAP programs for re-usability with Exception Handling

Introduction: Macros in ABAP are set of statements that are defined within DEFINE  and END-OF_DEFINITION within the program where we wished to use this macro. We can use the Macros after the Macro definition only in the same program in which it is defined. Macros are used for reusing same calculation in many places of same program instead of writing the same set of statements for same calculation.

In this article, i want to explain how we can define a Macro for different types of calculations and achieve exception handling within Macro definition.

Lets define a Macro with addition of two variables, subtraction two variables and division of one variable with another one. So that you can use the same macro within the program for different calculations by passing corresponding parameters.

Step 1 : Go to T Code SE38 and create Executable Program with name YTEST.

*&———————————————————————*
*& Report YTEST
*&———————————————————————*
*&
*&———————————————————————*
REPORT ytest.

***data declaration
DATA: addi TYPE i,
subt TYPE i,
divi TYPE f.

***constants declaration
CONSTANTS: num1 TYPE i VALUE 100,
num2 TYPE i VALUE 200,
num3 type i value 0.

***definition of macro
DEFINE tests.
TRY.
&1 = ( &2 + &3 ) + ( &4 – &5 ) + ( &6 / &7 ).
CATCH cx_sy_arithmetic_error.
CLEAR &1.
ENDTRY.
END-OF-DEFINITION.

***addition of two values 100 and 200
***      &1      &2      &3     &4  &5  &6  &7
tests addi   num1  num2   0    0    0    0 .

***subtraction of two values 100 and 200
***     &1     &2    &3   &4    &5    &6   &7
tests subt    0      0  num1 num2   0    0 .

***division of two values 100 and 200
***     &1    &2  &3  &4  &5  &6     &7
tests divi    0     0    0    0   num1 num2 .

***division of two values 100 and 0
***     &1    &2  &3  &4  &5   &6    &7
tests divi    0     0    0    0   num1 num3 .

***displaying calcullated values
WRITE: ‘Addition of’,num1,‘&’,num2,‘:’,addi,
/ ‘Subtraction of’,num1,‘&’ ,num2,‘:’, abs( subt ), “eliminating (-) sign
/ ‘Division of ‘,num1,‘by’,num2,‘:’, floor( divi ),  “rouding to lower value
/ ‘Division of ‘,num1,‘by’,num3,‘:’, floor( divi ).  “rouding to lower value

SAP ABAP Study Materials, SAP ABAP Learning, SAP ABAP Tutorials and Materials

SAP ABAP Study Materials, SAP ABAP Learning, SAP ABAP Tutorials and Materials

In the above program YTEST, we defined the Macro TESTS with below formula.

&1 = ( &2 + &3 ) + ( &4 – &5 ) + ( &6 / &7 ).

In this case when you want o calculate the addition of two parameters in the program, use the Macro by passing required parameters  to &1, &2, &3 and 0’s to other parameters. here &1 refers addi ( result variable ). 

***addition of two values 100 and 200
***      &1      &2      &3     &4  &5  &6  &7
tests addi   num1  num2   0    0    0    0 .

similarly for other calculations as well.

finally we can handle the exceptions also in the Macro definition by using the try clause .

TRY.
&1 = ( &2 + &3 ) + ( &4 – &5 ) + ( &6 / &7 ).
CATCH cx_sy_arithmetic_error.
CLEAR &1.
ENDTRY.

when we pass the Macro parameters with possibility of zero values and there is a chance of raising division by zero error. we can use arithmetic exception class to catch it in try catch block.

***division of two values 100 and 0
***     &1    &2  &3  &4  &5   &6    &7
tests divi    0     0    0    0   num1 num3 .

Above we are using Macro by passing parameters &1 i.e divi (result variable)  , &6 as 100 and &7 as 0 value and finally it will result 0 value to variable divi.

Click on F8 (execute)

SAP ABAP Study Materials, SAP ABAP Learning, SAP ABAP Tutorials and Materials

No comments:

Post a Comment