[ABAP] OO: Instanzkonstruktor, funktionale Instanzmethode, Exceptionhandling

* Einfache Klasse für Demo des Konstruktors, Instanzmethode, Exceptionhandling
CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
* Instanzkonstruktor mit Übergabeparameter
* Propagiert Exception vom Typ cx_sy_create_object_error
    METHODS:
      constructor
        IMPORTING
          i_name TYPE string
        RAISING
          cx_sy_create_object_error.

* funktionale Instanzmethode, gibt Namen zurück
    METHODS:
      get_name RETURNING VALUE(rv_name) TYPE string.

  PRIVATE SECTION.
* globale, private Variable zur Speicherung des Namens
    DATA: gv_name TYPE string.
ENDCLASS.

CLASS lcl_demo IMPLEMENTATION.
  METHOD constructor.
* prüfen, ob Name leer
    IF i_name = ''.
* Exception werfen
      RAISE EXCEPTION TYPE cx_sy_create_object_error.
    ELSE.
      gv_name = i_name.
    ENDIF.
  ENDMETHOD.

  METHOD get_name.
* Namen zurückgeben
    rv_name = gv_name.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
* Exception provozieren
  TRY.
      DATA(o_ref) = NEW lcl_demo( i_name = '' ).

      WRITE: / o_ref->get_name( ).

    CATCH cx_sy_create_object_error INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.
  
* "normale" Abarbeitung
  TRY.
      DATA(o_ref) = NEW lcl_demo( i_name = 'Test' ).

      WRITE: / o_ref->get_name( ).

    CATCH cx_sy_create_object_error INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.