Defining Your Own Exception Classes

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Define your own exception classes

Definition of Your Own Exception Classes

There are lots of predefined system exception classes whose names all begin with cx_sy. However, these classes describe technical errors and do not reflect the semantics of your application.

In a flight application, a class may need an exception that describes a situation where a particular combination of an airline and flight number does not exist.

To define an exception class of your own, create a new class that inherits from the class cx_static_check.

In this video, you will see how to create, prepare, attach, and raise an exception.

Try It Out: Definition of Your Own Exception Classes

Create an exception class ZSD4D01_EXCEPTIONS as follows:

  1. Press Ctrl + Shift + N to create a new ABAP Repository Object.
  2. Enter the filter text MESSAGE and double-click the entry Message Class in the hit list.
  3. Assign the message class to a valid package. Enter the name ZS4D401_EXCEPTIONS and a description, and then press Next.
  4. Select an appropriate transport request and choose Finish.
  5. Enter the message number 001 and the text Connection &1 &2 does not exist.
  6. Save the message class.
  7. Create a new class that implements the interface IF_OO_ADT_CLASSRUN.
  8. Switch to the Local Types tab and paste the following code snippet into the editor:
    Code snippet
    
    CLASS lcx_no_connection DEFINITION INHERITING FROM cx_static_Check.
    PUBLIC SECTION.
    interfaces if_t100_message.
    
    METHODS constructor
    IMPORTING
    textid LIKE if_t100_message=>t100key OPTIONAL
    previous LIKE previous OPTIONAL
    airlineid TYPE /DMO/CARRIER_ID OPTIONAL
    connectionnumber TYPE /DMO/CONNECTION_ID OPTIONAL.
    CONSTANTS:
    BEGIN OF lcx_no_connection,
    msgid TYPE symsgid VALUE 'ZS4D401_EXCEPTIONS',
    msgno TYPE symsgno VALUE '001',
    attr1 TYPE scx_attrname VALUE 'AIRLINEID',
    attr2 TYPE scx_attrname VALUE 'CONNECTIONNUMBER',
    attr3 TYPE scx_attrname VALUE 'attr3',
    attr4 TYPE scx_attrname VALUE 'attr4',
    END OF lcx_no_connection.
    
    DATA airlineid TYPE /dmo/carrier_id READ-ONLY.
    DATA connectionnumber TYPE /dmo/connection_id READ-ONLY.
    
    ENDCLASS.
    
    CLASS lcx_no_Connection IMPLEMENTATION.
    METHOD constructor.
    
    super->constructor( previous = previous ).
    
    me->airlineid = airlineid.
    me->connectionnumber = connectionnumber.
    
    CLEAR me->textid.
    IF textid IS INITIAL.
    if_t100_message~t100key = lcx_no_connection.
    ELSE.
    if_t100_message~t100key = textid.
    ENDIF.
    
    
    ENDMETHOD.
    
    ENDCLASS.
    
    CLASS lcl_connection DEFINITION.
    PUBLIC SECTION.
    METHODS constructor
    IMPORTING
    i_airlineid TYPE /dmo/carrier_id
    i_connectionnumber TYPE /dmo/connection_id
    RAISING lcx_no_connection.
    
    
    PRIVATE SECTION.
    DATA AirlineId TYPE /dmo/carrier_id.
    DATA ConnectionNumber TYPE /dmo/connection_id.
    DATA fromAirport TYPE /dmo/airport_from_id.
    DATA toAirport TYPE /dmo/airport_to_id.
    ENDCLASS.
    
    
    CLASS lcl_Connection IMPLEMENTATION.
    
    
    METHOD constructor.
    DATA fromairport TYPE /dmo/airport_from_Id.
    DATA toairport TYPE /dmo/airport_to_id.
    
    
    SELECT SINGLE FROM /dmo/connection
    FIELDS airport_from_id, airport_to_id
    WHERE carrier_id = @i_airlineid
    AND connection_id = @i_connectionnumber
    INTO ( @fromairport, @toairport ).
    
    
    IF sy-subrc <> 0.
    RAISE EXCEPTION TYPE lcx_no_connection
    EXPORTING
    airlineid = i_airlineid
    connectionnumber = i_connectionnumber.
    ELSE.
    me->connectionnumber = i_connectionnumber.
    me->fromairport = fromairport.
    me->toairport = toairport.
    ENDIF.
    ENDMETHOD.
    ENDCLASS.
    Expand
  9. Switch to the Global Class tab and paste the following code snippet into the implementation of the main( ) method:
    Code snippet
    
    DATA connection TYPE REF TO lcl_connection.
    DATA exception TYPE REF TO lcx_no_connection.
    
    TRY.
    connection = NEW #( i_airlineid = 'XX' i_connectionnumber = '0000' ).
    CATCH lcx_no_connection into exception.
    out->write( exception->get_text( ) ).
    ENDTRY.
    Expand
  10. Activate the class by pressing Ctrl + F3.
  11. Execute the class by pressing F9. Play around with the source code to familiarize yourself with it.

Log in to track your progress & complete quizzes