Quantcast
Channel: SCN : All Content - ABAP Development
Viewing all articles
Browse latest Browse all 8332

How to catch BCD_OVERFLOW error when passing value to formal parameter?

$
0
0

Hi,

 

catching runtime error BCD_OVERFLOW exception is simple. However, it's not possible to catch this error directly, if it results from assigning too big value to the formal parameter.

Let's assume simple code with local class lcl_calculator implementing functional method add with two input parameters i_op1 and i_op2 both of type i and result value r_result of type i as well.

 

The following code dumps, without the exception being caught:

 

DATA:
   lo_calculator TYPE REF TO lcl_calculator,
   l_result TYPE i.

START-OF-SELECTION.
   TRY.
       CREATE OBJECT lo_calculator.
       l_result = lo_calculator->add(
         i_op1 =
10000000000
         i_op2 = 1 ).
       WRITE:/ l_result.
     CATCH cx_sy_conversion_overflow.
       WRITE:/ 'Error'.
   ENDTRY.


To solve this, the workaround has to be implemented with checking the values being passed to the method before the actual call is made:


DATA:
   lo_calculator TYPE REF TO lcl_calculator,
   l_result TYPE i,
   l_op1 TYPE i,
   l_op2 TYPE i.


START-OF-SELECTION.
   TRY.

       l_op1 = 10000000000.
       l_op2 = 1.
      

       CREATE OBJECT lo_calculator.
       l_result = lo_calculator->add(
         i_op1 = l_op1
         i_op2 = l_op2 ).
       WRITE:/ l_result.
     CATCH cx_sy_conversion_overflow.
       WRITE:/ 'Error'.
   ENDTRY.


It's the same with the function module call, so it's general unit interface issue. Also, using the exception handling related to the CALL METHOD command does not help here as it's not wrong parameter TYPING which causes the error. It's the VALUE of correctly typed parameter that causes the error.

The CATCH apparently reacts different ways when the assignment is made to the variable and to the formal parameter of the unit. Any idea how to solve the above without using that workaround?


Thank you

Michal


Viewing all articles
Browse latest Browse all 8332

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>