SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

OO ABAP - Instance Constructors can Raise Exceptions

OO ABAP - Instance Constructors can Raise Exceptions

 

This program contains a class C1 which contains an instance constructor method. It accepts an import parameter, NUM.

If it is lesser than 7, an exception is raised, which is properly handled by specifying some sy-subrc value at the time of CREATE OBJECT statement and later handled properly.


CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS : constructor IMPORTING  num TYPE i
                          EXCEPTIONS e1 .
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD constructor.
    IF num LT 7.
      RAISE  e1.
    ENDIF.
  ENDMETHOD.
ENDCLASS.
*************** main program **************
START-OF-SELECTION.
  DATA: obj1 TYPE REF TO c1.
  CREATE OBJECT obj1
    EXPORTING
      num = 5
    EXCEPTIONS
      e1  = 2.
  IF sy-subrc = 2.
    WRITE:/5 'Exceptions raised'.
  ENDIF.

Output –>

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in