Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

catching data conversion errors

Former Member
0 Likes
5,961

Hi there,

I am trying to catch conversion errors. Always from string to an arbitrary target field, example for STRING -> N(10) below:


DATA:
      g_string TYPE string VALUE 'A12345Q',
      g_num(10) TYPE n.

g_num = g_string. "(A)

CATCH SYSTEM-EXCEPTIONS convt_no_number = 5 OTHERS = 9.
  IF g_num NE g_string. "(B)
  ENDIF.
ENDCATCH.

IF sy-subrc <> 0.
  WRITE: / 'Format error'.
ENDIF.

The problem is after (A) g_num contains only digits and no conversion error is raised.

At (B), short dump is raised, despite the CATCH ENDCATCH block.

Is there any way to perform such assignment and be able to catch proper conversion exceptions ?

More less like:


FIELD-SYMBOLS:
<g_symbol1> TYPE ANY,
<g_symbol2> TYPE ANY.

<g_symbol1> = <g_symbol2>. " <---- if conversion error, do something

1 ACCEPTED SOLUTION
Read only

ThomasZloch
Active Contributor
0 Likes
3,013

(A) is not a conversion error, the system behaves as defined for a CHAR (or STRING) to NUMC conversion.

(B) cannot be caught, because it is an implicit conversion, here inside a logical expression.

Thomas

8 REPLIES 8
Read only

Former Member
0 Likes
3,013

Hi,

Try this.

Use the condition inside the 'TRY' and 'ENDTRY'.

TRY.

IF g_num NE g_string. "(B)

ENDIF.

CATCH SYSTEM-EXCEPTIONS.

<as per your requirements>

ENDTRY.

Hope this may be helpful.

Regards,

Sharin.

Read only

0 Likes
3,013

CATCH SYSTEM-EXCEPTIONS and TRY cannot be used simultaneously.

Read only

0 Likes
3,013

Hi Bartosz,

I just gave the syntax for 'TRY' and 'CATCH' to try it.................didnt mean exactly the exceptions.

Thanks,

Sharin.

Read only

ThomasZloch
Active Contributor
0 Likes
3,014

(A) is not a conversion error, the system behaves as defined for a CHAR (or STRING) to NUMC conversion.

(B) cannot be caught, because it is an implicit conversion, here inside a logical expression.

Thomas

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,013

 try.
            <target> = <source>.
          catch cx_sy_conversion_no_number.
        endtry.
Read only

Former Member
0 Likes
3,013

@Thomas Zloch

Too bad, (B) can't be caught, that's exacly what I need.

@Keshav.T

According to Thomas' post, normal conversion between STRING and NUMC is performed, no error.

@Sharin Varghese

Well, You've put 'CATCH SYSTEM-EXCEPTIONS' between TRY...ENDTRY hence my response

---

Still waiting for some suggestions. Have some more minutes of faith that it can be done somehow.

Read only

0 Likes
3,013

Is STRING to NUMC your only conversion? You could try to move the content from string1 to numc1, then move numc1 to string2, then compare string1 and string2, if different, then there was a "conversion error".

Thomas

Edit: just saw that NUMC was only an example, well, maybe you can find a similar way for other conversions.

Edit: http://help.sap.com/abapdocu_70/en/ABENCONVERSION_ELEMENTARY.htm

Read only

0 Likes
3,013

Oh well, I've tried to get an universal solution. As I am going to work on flat tab delimited files, I will just cover numerics and characters / strings. Thanks.