Table of Contents
A programmer always tries to code in such a way that his code is bug free and full proof. But there are certain scenarios, where his codes can fail. In such case, if the coder already knows the scenarios, then he handle them in form of Error handling. But, in case of situations, where an error can be generated due to unknown situations and left un-handled can cause serious dumps. For these scenarios, we have exception handling in SAP. In this article we will explore all scenarios of Exception and Error Handling in ABAP Reports
Exception and Error Handling in SAP ABAP Reports
As discussed above, a coder is required to handle known issues as well as intended error messages. These messages are shown to user in the output via MESSAGE statement.
Different ways to catch Errors/Exception
ABAP provides various ways to catch errors in an ABAP program.
Using System Variable
The simplest way is to use their system variables.
SY-SUBRC EQ 0. "This statement indicates that an operation completed successfully. SY-SUBRC NE 0. "This statement indicates that an operation has failed.
This way works directly if the entire code of report was process within the same program. It means, it cannot catch an error directly in case you have processed a Function Module or ABAP Class statements. For those entity, we need to raise exception from their end, and then exception can be handled in the report and processed accordingly.
Catching Errors based on Exception
In case, we implement a function module or method of a class, then it is important to raise exception from these classes and then we can catch these exceptions in report and show respective error.
CALL FUNCTION 'Test_Function' EXPORTING DELIMITER = ':' STRING = lv_string IMPORTING HEAD = lv_head TAIL = lv_tail EXCEPTIONS NOT_FOUND = 1 OTHERS = 2. CASE SY-SUBRC. WHEN 1. ... // Show Error Message WHEN 2. ... // Show Error Message WHEN OTHER. // Show Error Message ENDCASE.
Using TRY CATCH ENDTRY
In case an exception is raised via a method of class, or an unexpected exception is raised, in these cases, we use TRY CATCH functionality of SAP ABAP. This is one of the best full proof way to handle all types of exceptions.
TRY. //Call your class or function module here CATCH CX_SY_ZERODIVIDE INTO O_REF. // The exception you have raised there MESSAGE “Your Error Message”. ENDTRY.
Different ways to show Messages
Using ABAP Statement
MESSAGE ‘<Enter Your Text here>’ TYPE ‘Enter the type of Message here’.
Types of Message
ABAP provides the following 6 types of messages:
|A||Termination||This message is shown during program termination.|
|E||Error||This message is shown during Error.|
|I||Information||This message is used to show any information.|
|S||Success||This shown in the status of the Output screen.|
|W||Warning||It behaves like an error message.|
|X||Exit||It causes a short dump with error message.|
Using Predefined Function Modules
ABAP provides following function modules that can be used to store, format and show messages altogether:
|MESSAGES_INITIALIZE||To Initialize the messages.|
|MESSAGE_STORE||To Store the messages to be displayed.|
|MESSAGES_SHOW||To Display all the messages together on a pop up|
|FORMAT_MESSAGE||To Format the messages|
|HR_DISPLAY_ERROR_LIST||To display all the error messages|
- Using Message Statement
MESSAGE 'This is an error message' TYPE 'E'.
- Using Multiple functions to store and show messages:
" It is Initialized only initially... * Initialize your messages CALL FUNCTION 'MESSAGES_INITIALIZE' EXCEPTIONS log_not_active = 1 wrong_identification = 2 OTHERS = 3. "One by one append all your messages here PERFORM store_messages USING 'E' w_pn w_batch2 w_werks ' ' w_msgno. FORM store_messages USING p_msgty p_msgv1 p_msgv2 p_msgv3 p_msgv4 p_txtnr. IF p_msgty EQ 'E'. w_err_fg = 'X'. ENDIF. * Store all your messages meant to be displayed CALL FUNCTION 'MESSAGE_STORE' EXPORTING arbgb = 'ZCCH001' msgty = p_msgty msgv1 = p_msgv1 msgv2 = p_msgv2 msgv3 = p_msgv3 msgv4 = p_msgv4 txtnr = p_txtnr EXCEPTIONS message_type_not_valid = 1 not_active = 2 OTHERS = 3. ENDFORM. " STORE_MESSAGES "In the end fetch all your message and show them altogether * This displays all the messages in a popup CALL FUNCTION 'MESSAGES_SHOW' EXPORTING show_linno = ' ' IMPORTING e_exit_command = wa_exit_command EXCEPTIONS inconsistent_range = 1 no_messages = 2 OTHERS = 3.
- Using HR_DISPLAY_ERROR_LIST
DATA: it_error TYPE STANDARD TABLE OF HRERROR,"TABLES PARAM wa_error LIKE LINE OF it_error . DATA(ld_no_popup) = 'some text here'. DATA(ld_no_print) = 'some text here'. DATA(ld_no_img) = 'some text here'. DATA(ld_no_msgno) = 'some text here'. DATA(ld_linesize) = '123 '. DATA(ld_listheader) = 'Check type of data required'. DATA(ld_colheader) = 'Check type of data required'. DATA(ld_hidemsg) = 'some text here'. "populate fields of struture and append to itab append wa_error to it_error. . CALL FUNCTION 'HR_DISPLAY_ERROR_LIST' * EXPORTING * no_popup = ld_no_popup * no_print = ld_no_print * no_img = ld_no_img * no_msgno = ld_no_msgno * linesize = ld_linesize * listheader = ld_listheader * colheader = ld_colheader * hidemsg = ld_hidemsg * TABLES * error = it_error EXCEPTIONS INVALID_LINESIZE = 1 . " HR_DISPLAY_ERROR_LIST IF SY-SUBRC EQ 0. "All OK ELSEIF SY-SUBRC EQ 1. "Exception "Add code for exception here ENDIF.