Sunday, September 11, 2016

Exception Handling

Definition:
Exceptions are the errors that occurs during the execution of the program that interrupts the normal flow of control.

Types of Exceptions:

All exception classes are inherited from global class CX_ROOT and the classes that are directly inherited are

CX_STATIC_CHECK:
It is checked by both the compiler and run-time system. If any exception of this category found and not handled locally inside the procedure, it has been declared in the RAISING clause of the procedure’s interface

CX_DYNAMIC_CHECK:
It is checked only at run-time system. when the exceptions tries to leave a procedure that it has been declared in RAISING class. Generally all system exceptions like CX_SY_ZERO_DIVIDE will come under these exceptions

CX_NO_CHECK:
This type of exceptions will be chosen if the exception can occur almost everywhere. These exceptions are always implicitly declared.
Neither the compiler nor the run-time system performs any interface checks.


Step-by-Step Approach for creation of Exception Classes

Step 1:
Go to Transaction SE24, enter class name as “ZCX_EXCEP_CLS“ .
For all exception classes “CX” will be the prefix.
Choose “Create“  button.  

Step 2:  
Popup will be displayed in the next screen. Enter the super class name “CX_STATIC_CHECK”.
Exception classes are derived from one of pre-defined basic classes CX_STATIC_CHECK, CX_DYNAMIC_CHECK or CX_NO_CHECK. So enter one of the basic classes as super classes.
Instead of creating new texts, you want can use the texts from an existing message class (which are defined in TCODE SE91). Then select the “With Message Class” checkbox.
Choose Save Button.  

Step 3:  
The initial screen of exception class will be display with some predefined attributes/Methods.
Exception classes inherit the following attributes from base class “CX_ROOT“

Step 4:
Using Tcode SE91 a message class““ is created with message 000 as shown below. Used this message class in Exception Class

Step 5:
In Exception Class “ZCX_EXCEP_CLS”, create attributes MSGV1, MSGV2, MSGV3, and MSGV4 under attributes tab of the class builder.

Step 6:
To Insert Exception Texts from Message Classes Choose “Texts” tab in the class builder.
Then Place the cursor on the default Exception ID “ZCX_EXCEP_CLS” and then click on the  

Step 7:
Popup will be displayed.  Enter Message Class “ZEXC_MSG” and Message Number “000”.

Step 8:
If you want parameterized Exception Texts, assign the attributes of an Exception Class to the placeholders that exist in attributes tab of exception class.
Placeholders (‘&’) created here can be dynamically be populated when run-time exceptions are created using ABAP values via exception class attributes.
Here I assigned MSGV1, MSGV2, MSGV3 and MSGV4 to the placeholders.

Click on  button.

Step 9:
The values of the attributes belonging to exception will be linked to a text from message class.
‘&’ will be replaced with corresponding values of attributes (i.e. MSGV1, MSGV2, MSGV3 and MSGV4) defined in parameterized exceptions in STEP 7.
Save and active the Exception class
Note:  Exception texts are generally language-specific and have to be translated.

Step 10:
Calling Exception Class inside the program to handle exceptions
Ex:
DATA: ex_ref TYPE REF TO cx_root.
DATA: message_text TYPE string.

TRY.
    RAISE EXCEPTION TYPE zcx_excep_cls
                EXPORTING msgv1 = 'For'
                                        msgv2 = 'Handling'
                                        msgv3 = 'Exceptions '
                                        msgv4 = 'In Program '.

  CATCH zcx_excep_cls INTO ex_ref.
    message_text = ex_ref->get_text( ).
    WRITE message_text.
ENDTRY.

Output:
‘&’ will be replaced with the texts assigned to attributes (MSGV1, MSGV2, MSGV3 and MSGV4) of exception class.

Step 11:
While creating Exception class, uncheck the “With Message Class” check box, if you want to create new exceptions texts.
The texts will be created and stored in the text management OTR. It is not possible to use both text variants in an exception class.

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...