‎2010 Dec 17 11:27 AM
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
‎2010 Dec 17 11:48 AM
(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
‎2010 Dec 17 11:32 AM
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.
‎2010 Dec 17 11:42 AM
CATCH SYSTEM-EXCEPTIONS and TRY cannot be used simultaneously.
‎2010 Dec 17 12:08 PM
Hi Bartosz,
I just gave the syntax for 'TRY' and 'CATCH' to try it.................didnt mean exactly the exceptions.
Thanks,
Sharin.
‎2010 Dec 17 11:48 AM
(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
‎2010 Dec 17 11:48 AM
try.
<target> = <source>.
catch cx_sy_conversion_no_number.
endtry.
‎2010 Dec 17 12:26 PM
@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.
‎2010 Dec 17 12:37 PM
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
‎2010 Dec 20 8:11 AM
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.