Skip to content

Instantly share code, notes, and snippets.

@Gong-Bao-Chicken
Last active June 27, 2017 14:10
Show Gist options
  • Save Gong-Bao-Chicken/9cb0c5d5200ca10c129afe0726974093 to your computer and use it in GitHub Desktop.
Save Gong-Bao-Chicken/9cb0c5d5200ca10c129afe0726974093 to your computer and use it in GitHub Desktop.
How to use Class Exeptions in ABAP
* Prerequirements:
*
* 1. Create an Exception Class /{PACKAGE}/CX_{NAME} in the working package.
* 2. Open the Exception Class and add Interface: "IF_T100_MESSAGE" to it.
* 3. Create a 'Nachrichtenklasse' with the name /{Package}/(...)_{NAME}_NK
*
*Then:
*
* 1. Open the working Class and select a method to which to add an exception.
* 2. Open the Parameter Section for the Method then choose the exception section and add the exception name /{PACKAGE}/CX_{NAME} to the Method!
* 3. Open the methods program code and add this data dekleration (structure for the output).
* 4. Fill the structure with information to be passed to the user.
* 5. Raise the exception in the method after an error or errouros state was entered using the syntax RAISE EXCEPTION TYPE /{PACKAGE}/CX_{NAME} EXPORTING ________ .
********************************************************
* EXAMPLE: CLASS/METHOD
********************************************************
DATA: ls_t100_key type scx_t100key.
*... some code to be executed ...
IF SY-SUBRC <> 0. "error
ls_t100_key-msgid = '/PACKACKGE/test_messegeclass'.
ls_t100_key-msgno = 000. " TEXT TO BE PRINTED FROM MESSEGE CLASS! (USE & FOR ATTRIBUTES)
RAISE EXCEPTION TYPE /package/cx_text_exception_class EXPORTING textid = ls_t100_key.
ENDIF.
********************************************************
* EXAMPLE: Optional Implement this as Method in the Class:
********************************************************
class-methods THROW_EXCEPTION
importing
!IV_MSG_ID type SYMSGID optional
!IV_MSG_NUMBER type SYMSGNO
!IV_ATTRIBUT1 type SCX_ATTRNAME optional
!IV_ATTRIBUT2 type SCX_ATTRNAME optional
!IV_ATTRIBUT3 type SCX_ATTRNAME optional
!IV_ATTRIBUT4 type SCX_ATTRNAME optional
raising
/PSI/CX_DYNAMIC_ALV .
method THROW_EXCEPTION.
DATA: ls_t100_key type scx_t100key.
if iv_msg_id is initial.
ls_t100_key-msgid = 'nachrichtenklasse'.
else.
ls_t100_key-msgid = iv_msg_id.
endif.
ls_t100_key-msgno = iv_msg_number.
ls_t100_key-attr1 = iv_attribut1.
ls_t100_key-attr2 = iv_attribut2.
ls_t100_key-attr3 = iv_attribut3.
ls_t100_key-attr4 = iv_attribut4.
RAISE EXCEPTION TYPE /package/cx_name EXPORTING textid = ls_t100_key.
endmethod.
* 6. Declare the exception as reference in the program code.
* 7. Finally wrap the messege call with a TRY. CATCH. ENDTRY. block.
********************************************************
* EXAMPLE: PROGRAM
********************************************************
data: lx_exception type ref to /package/cx_text_exception_class.
Try.
Class->Method()
CATCH /package/cx_text_exception_class into lx_exception.
MESSAGE lx_exception TYPE 'I' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
Endtry.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment