cancel
Showing results for 
Search instead for 
Did you mean: 

Can we use TRY-CATCH when using constructor expressions?

sh4il3sh
Participant

Hello Experts,

I have below way to fetch UUID currently which handles exception:

    TRY .
        DATA(lv_uuid) = cl_system_uuid=>if_system_uuid_rfc4122_static~create_uuid_c36_by_version( version = 4 ).
      CATCH cx_uuid_error INTO gx_root.
        gv_msgtxt = gx_root->get_text( ).
    ENDTRY.

Bbut I wanted to write this inline.
I am able to do so but TRY-CATCH goes for a toss and warnings about exception not handled start to appear.
DATA(lt_bp_gen)  =
    VALUE mdg_bp_bpsuiterplct_req_ms_tab( (
             message_header = VALUE #( uuid-content = cl_system_uuid=>if_system_uuid_rfc4122_static~create_uuid_c36_by_version( version = 4 )
* uuid-content = lv_uuid (dont want to use this way)
sender_business_system_id = 'INFO_29_GEN' ) ) ).
anyway to write TRY-CATCH in same line? TIA

Side Question: do we have any standard Data Element for the value 'true'? like we have abap_true for 'X'.

Regards,
Shailesh
Sandra_Rossi
Active Contributor
0 Kudos

Impossible. You must have TRY-CATCH outside the constructor expression.

Why do you want to have it inside?

sh4il3sh
Participant
0 Kudos

say I have a case where I want to consume two different exception classes and catch error for one more field other than UUID, which would cover different exception.

I see as a blanket I can call CX_ROOT and cover all in one go, but what if I want to do field specific?

maybe the question itself is wrong, I am just curious.

Sandra_Rossi
Active Contributor

Oh, thanks, I get it, it makes sense. I never met this situation, usually an Exception is an exception, not to continue the normal flow.

This concept is otherwise managed via a Resumable Exception, but the exception must be raised as Resumable (RAISE RESUMABLE ...) Exceptions are usually not resumable (it consumes memory to keep context). I don't even know if a resumable exception can be continued into a constructor expression, interesting test to do...

Accepted Solutions (1)

Accepted Solutions (1)

michał_badura
Participant

I don't think there is any way to use TRY - CATCH - ENDTRY within a construction operator or procedure call.

But what You can do, is encapsulate Your code in a separate method, in which You can then use TRY - CATCH - ENDTRY.

As for true - why don't You define Your own constant? It would be S/4 and cloud ready and resistant to any simplifications. 😉

sh4il3sh
Participant
0 Kudos

I was sure there must be a standard for something as simple like this so why have redundancy, found it sai_true and sai_false.
to carry these over in cloud and keep resistant is SAP's problem. 😛
anway we have SELECTION-SCREEN here so this will never make it to cloud.

michał_badura
Participant
0 Kudos

I see Your point, sh4il3sh. I personally would however prefer a redundancy over violating the domain in this case. What I mean: sai_true comes from package SAI_PROXY_PUBLIC, which is "Proxy Generation: Public Interfaces". Does Your project have something in common with proxy generation? If not, why are You mixing the domains?!

Just a rhetorical question and my point of view.

Thanks for accepting my answer anyway.

Greetings!

sh4il3sh
Participant
0 Kudos

I actually am consuming standard proxy in my program.

Answers (1)

Answers (1)

juliandanho
Participant

Hi sh4il3sh

you can catch the exception over the VALUE statement:

TRY.
DATA(lt_bp_gen) = VALUE mdg_bp_bpsuiterplct_req_ms_tab( ( message_header = VALUE #( uuid-content = cl_system_uuid=>if_system_uuid_rfc4122_static~create_uuid_c36_by_version( version = 4 )
sender_business_system_id = 'INFO_29_GEN' )
) ).
CATCH cx_uuid_error INTO DATA(gx_root).
gv_msgtxt = gx_root->get_text( ).
ENDTRY.

Hope this will help you.

Kind regards
Julian Danho

sh4il3sh
Participant
0 Kudos

Thanks for answering, Yes I could, this works everywhere!
I didnt want to have the whole block(which will be big) under TRY.