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

Catch in Loop w/o exiting loop.

kmoore007
Active Contributor
0 Likes
3,121

Hi,

I'm performing a LOOP at table. ENDLOOP. Within the loop I have a TRY. ENDTRY. CATCH. This is so I can perform a arithmatic statement and catch any errors. The problem is, if an error occurs it exits the LOOP. I don't want to exit the entire LOOP statement. I just want to continue on with the next record in my internal table. But instead, the loop is exited.

Any ideas?

Example code:


  LOOP AT t_pcts ASSIGNING <wa_pcts>.
        TRY.
            <wa_pcts>-percent = <wa_pcts>-expprod / <wa_pcts>-actprod.
          CATCH cx_root INTO oref.
            text = oref->get_text( ).
        ENDTRY.
  ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,386

Hi,

check if your program is activated. TRY CATCH in LOOP works fine.

For my program:


DATA gt_vbrp TYPE TABLE OF vbrp WITH HEADER LINE.
APPEND INITIAL LINE TO gt_vbrp.
APPEND INITIAL LINE TO gt_vbrp.
APPEND INITIAL LINE TO gt_vbrp.
LOOP AT gt_vbrp.
  TRY.
      gt_vbrp-fkimg = 100 / gt_vbrp-fkimg.
    CATCH cx_root.
      WRITE: / 'Exception'.
  ENDTRY.
  WRITE: / sy-tabix.
ENDLOOP.

I get correct result:


Exception
         1
Exception
         2
Exception
         3

Program does not interrupt the loop.

Regards,

--

Przemysław

3 REPLIES 3
Read only

Former Member
0 Likes
1,387

Hi,

check if your program is activated. TRY CATCH in LOOP works fine.

For my program:


DATA gt_vbrp TYPE TABLE OF vbrp WITH HEADER LINE.
APPEND INITIAL LINE TO gt_vbrp.
APPEND INITIAL LINE TO gt_vbrp.
APPEND INITIAL LINE TO gt_vbrp.
LOOP AT gt_vbrp.
  TRY.
      gt_vbrp-fkimg = 100 / gt_vbrp-fkimg.
    CATCH cx_root.
      WRITE: / 'Exception'.
  ENDTRY.
  WRITE: / sy-tabix.
ENDLOOP.

I get correct result:


Exception
         1
Exception
         2
Exception
         3

Program does not interrupt the loop.

Regards,

--

Przemysław

Read only

kmoore007
Active Contributor
0 Likes
1,386

You can also use 'CONTINUE' after the CATCH statement I discovered.

Read only

Former Member
0 Likes
1,386

Hi,

CONTINUE after CATCH is unnecessary - program is processing next row automatically.

Regards,

--

Przemysław