ABAP on Cloud Exception Handling

Exceptions are way to communicate that something went wrong. An exception is a problem that arises during the execution of a program. When an exception occurs the normal flow of the program is disrupted and the program application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.

ABAP exception handling is built upon three keywords − RAISE, TRY, CATCH and CLEANUP.

Raising Exceptions 

Which situations can raise an exception ?

SITUATIONS WHICH RAISES EXCEPTION

Situations on the top raises a condition which results in a situation which cannot be handled by a program and results in abrupt termination of the program. Let’s see a how a program handles this situation through latest syntax

In new ABAP and in eclipse we cannot write a report through SE38 and we need to create class

So let’s create a class and make sure to implement the interface if_oo_adt_classrun, you can read more about the interface if_oo_adt_classrun HERE.

CLASS zcl_exception_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_oo_adt_classrun .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_EXCEPTION_CLASS IMPLEMENTATION.


  METHOD if_oo_adt_classrun~main.


  ENDMETHOD.
ENDCLASS.

Now we need to do the implementation, we will handle a situation where something is getting divided by 0, Here is the implementation is done in the global class and now our global class will look something like this

CLASS zcl_exception_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_oo_adt_classrun .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS ZCL_EXCEPTION_CLASS IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.

    CONSTANTS NUM1 TYPE I VALUE '10'.
    CONSTANTS NUM2 TYPE I VALUE '0'.

    TRY.
        Data(num3) = NUM1 / Num2.

      CATCH cx_sy_zerodivide.
        out->write( `Method call failed number cannot be divided by 0`     ).
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

Note we could have done the same implementation in a local class and then in that case we would have to create a object of local class in global class and call the methods inside global class, that example would look something like this

Below is the global class

CLASS zcl_exception_class DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_oo_adt_classrun .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_EXCEPTION_CLASS IMPLEMENTATION.


  METHOD if_oo_adt_classrun~main.

    CONSTANTS c_carrier_id TYPE /dmo/carrier_id       VALUE 'LH'.
    CONSTANTS c_connection_id TYPE /dmo/connection_id.

    DATA connection  TYPE REF TO lcl_connection.
    DATA connections  TYPE TABLE OF REF TO lcl_connection.

* Create Instance
**********************************************************************

    connection = NEW #(  ).


    TRY.
        connection->set_attributes(
          EXPORTING
            i_carrier_id    = c_carrier_id
            i_connection_id = c_connection_id
        ).

        APPEND connection TO connections.
        out->write( `Method call successful` ).
      CATCH cx_abap_invalid_value.
        out->write( `Method call failed`     ).
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

Implementation in local class

*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations

CLASS lcl_connection DEFINITION.
  PUBLIC SECTION.

* Attributes
    DATA carrier_id    TYPE /dmo/carrier_id.
    DATA connection_id TYPE /dmo/connection_id.
    


* Methods
    METHODS set_attributes
      IMPORTING
        i_carrier_id    TYPE /dmo/carrier_id  DEFAULT 'LH'
        i_Connection_id TYPE /dmo/connection_id
      RAISING
        cx_abap_invalid_value.


ENDCLASS.

CLASS lcl_connection IMPLEMENTATION.



  METHOD set_attributes.

    IF i_carrier_id IS INITIAL OR i_connection_id IS INITIAL.
      RAISE EXCEPTION TYPE cx_abap_invalid_value.
    ENDIF.

    carrier_id    = i_carrier_id.
    connection_id = i_connection_id.

  ENDMETHOD.

ENDCLASS.

Leave a comment