styleguides icon indicating copy to clipboard operation
styleguides copied to clipboard

MESSAGES in Exception

Open sdfraga opened this issue 6 years ago • 2 comments

Hello, First of all I would like to thank you for this amazing work! It's becoming the bible for any conscious ABAPER!

I would like to ask you your opinion about passing MESSAGES directly in the RAISE EXCEPTION command. From 7.50 we know that we can now convert messages into exception using the following:

RAISE EXCEPTION TYPE cx_demo_dyn_t100 MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

What would be the best practice here? My personal problem is that I have several systems and not all of them are in 7.50 so I have to switch strategies depending on the system.

In my opinion, this new form is better because you need fewer lines of code, gets more readable, and we avoid the creation of Message Classes to be used in the TEXT_ID property of the exception class.

Nevertheless, I believe this guide should have some hints on the topic. Do you agree?

Thanks again for your work! Sérgio

sdfraga avatar Nov 25 '19 13:11 sdfraga

I prefer using raises this way, but not all the time I realize. Just recently, I wanted to log "more" - And found out that inside the exception, you do not have access to the message you added to it with your syntax above. I haven't worked with a 7.52 system where you can do raise exception xxx using message, that would solve this problem.

openPhiL avatar Nov 28 '19 07:11 openPhiL

I would also add this! I constantly work with this type of exception handling. Additionally, I put all relevant variables (msgid, msgty & msgno) into constants defined in an interface. This way I think exceptions are easy to read AND maintain. Example: RAISE EXCEPTION TYPE zcx_dummy MESSAGE ID zif_dummy=>c_msg_class-general TYPE 'E' NUMBER zif_dummy=>c_general_msg-variable_not_found WITH variable.

AndreasHauer-STELACON avatar Apr 19 '20 19:04 AndreasHauer-STELACON