08-05-2015 12:28 PM
Hi,
I want to check if it's possible to convert one parameter in an other.
For example when I want to move a string to a data, the move should go in exception.
I was trying this with following code, but the code is just converting a string in a data, without going in exception.
DATA: lv_var1 type String.
DATA: lv_var2 type DATUM.
lv_var1 = 'Test'
TRY.
MOVE lv_var1 TO lv_var2.
CATCH cx_sy_conversion_error into Exception
RETURN Exception.
ENDTRY.
Result: lv_var2 = 'Test'
=> I want the program to check 2 parameters and return an exception, but this is not happening
08-05-2015 12:59 PM
Dear Tim,
As the datum type is a 8 charater field, you don't have a conversion error. Try to declare lv_var2 as a packed decimal:
data: lv_var2(8) type p.
and your exception will work.
If this suits you; please award points.
Regards,
Hans
08-05-2015 1:01 PM
Hi.
The problem is, that the Date-Field is a CHAR8 , so it is compatible with string and there is no exception. I have an idea, but I don't know, if it solves your problem:
With DESCRIBE FIELD you can check the Datatypes:
DESCRIBE FIELD lv_var1 TYPE lv_typ1. " LV_TYP1 = 'g'
DESCRIBE FIELD lv_var2 TYPE lv_typ2. " LV_TYP2 = 'D'
if lv_typ1 = lv_typ2.
....
endif.
But this tests, if the Types are identical, not only compatible. A char Field is not identical to a string.
The sap F1-Help shows you the values for every Datatype.
regards,
Peer
08-05-2015 3:02 PM
How is it possible that I can MOVE a String into an Integer or String?
None of both solutions are working for me :s
08-05-2015 3:26 PM
Hello.
That should be no problem:
DATA: lv_int TYPE i,
lv_string TYPE string.
lv_int = 15.
MOVE lv_int TO lv_string.
WRITE:/ lv_int,lv_string.
lv_string = '2'.
MOVE lv_string TO lv_int.
WRITE:/ lv_int,lv_string.
Output:
15 15
2 2
If the string has a non-numeric value, it will dump. But in this case you can use the TRY .. ENDTRY, as you wrote in the beginning.
lv_string = 'x'.
TRY.
MOVE lv_string TO lv_int.
WRITE:/ lv_int,lv_string.
CATCH cx_sy_conversion_error.
WRITE:/ 'error'.
ENDTRY.