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

New Syntax Read/ASSERT

0 Likes
10,009

Hello together,

I used a READ TABLE statement in combination with ASSIGNING and now replaced it by the following coding:

TRY.           

ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ]  TO <ls_fu_root>.         

CATCH cx_sy_itab_line_not_found.           

" TBD: Error Handling       

ENDTRY.

Directly after this read statement, there was an ASSERT sy-subrc = 0 in the old version.

How to enable this by using the new syntax? Can that be replaced if we now do something like ASSERT cx_sy_itab_line_not_found is bound in the CATCH statement?

Any Ideas?

Kind regards

1 ACCEPTED SOLUTION
Read only

michael_piesche
Active Contributor
7,190
  • Why are you trying to 'replace' everything?
  • The READ TABLE statement and the ASSERT statement are not directly LINKED to each other.
  • After a failed ASSIGN statement, the sy-subrc is still set to "4 Assignment not completed.", if the assignment couldnt be performed in various instances, so the ASSERT sy-subrc = 0. statement still works for your coding (unless you add more coding in between the ASSIGN and the ASSERT that could potentially change the sy-subrc):
TRY.           
  ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ]  TO <ls_fu_root>.         
CATCH cx_sy_itab_line_not_found.                 
ENDTRY.
ASSERT sy-subrc = 0.

In other instances, you might want to rewrite it to something like this, to make sure that the assignment was successfull. This way you dont rely on sy-subrc, which would also make it more robust and more straight forward:

UNASSIGN <ls_fu_root>.
TRY.           
  ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ]  TO <ls_fu_root>.         
CATCH cx_sy_itab_line_not_found.           
  " TBD: Error Handling       
ENDTRY.
ASSERT <ls_fu_root> IS ASSIGNED.
4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
7,190

As of 7.53, ASSERT has no replacement, hopefully.

If you understand what ASSERT means, don't replace it with TRY CATCH.

Read only

0 Likes
7,190

That means, it is not possible to replace the read statement when an assert follows, correct?

BR

Read only

michael_piesche
Active Contributor
7,191
  • Why are you trying to 'replace' everything?
  • The READ TABLE statement and the ASSERT statement are not directly LINKED to each other.
  • After a failed ASSIGN statement, the sy-subrc is still set to "4 Assignment not completed.", if the assignment couldnt be performed in various instances, so the ASSERT sy-subrc = 0. statement still works for your coding (unless you add more coding in between the ASSIGN and the ASSERT that could potentially change the sy-subrc):
TRY.           
  ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ]  TO <ls_fu_root>.         
CATCH cx_sy_itab_line_not_found.                 
ENDTRY.
ASSERT sy-subrc = 0.

In other instances, you might want to rewrite it to something like this, to make sure that the assignment was successfull. This way you dont rely on sy-subrc, which would also make it more robust and more straight forward:

UNASSIGN <ls_fu_root>.
TRY.           
  ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ]  TO <ls_fu_root>.         
CATCH cx_sy_itab_line_not_found.           
  " TBD: Error Handling       
ENDTRY.
ASSERT <ls_fu_root> IS ASSIGNED.
Read only

teshanappadoo
Participant
7,190

If you are using ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ] TO <ls_fu_root>.

then why not check sy-subrc = 0 or if you want to raise some exception then sy-subrc <> 0.

ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ] TO <ls_fu_root>.

if sy-subrc = 0.

" do something

endif,

OR

ASSIGN mt_fu_root_data[ key = <ls_dlv_kl_fu>-target_key ] TO <ls_fu_root>.

if sy-subrc <> 0.

" raise exception.

endif.

Refer to: https://blogs.sap.com/2015/10/25/abap-740-quick-reference/

Regards,

Teshan