Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Shortdump in exceeded BALANCE.

Hi all,

I have written the OOP program as below to SET_DEPOSIT, DEPOSIT and WITHDRAW.

There is a exception in the code to RAISE an event when withdrawal is more than the BALANCE.

However I always get a shortdump whenever the amount withdraw is exceeded the balance.

What should I do in the code to avoid shortdump. I know there is something missing in the code.

Please advice. Thanks.

*&---------------------------------------------------------------------*
*& Report  Z_OOP_ACCOUNT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  Z_OOP_ACCOUNT.

CLASS lcl_account DEFINITION.
  PUBLIC SECTION.
    DATA: BALANCE TYPE DMBTR.

    METHODS:
      SET_DEPOSIT IMPORTING NEW_BALANCE TYPE DMBTR,
      DEPOSIT     IMPORTING AMOUNT TYPE DMBTR
                  EXPORTING NEW_BALANCE TYPE DMBTR,
      WITHDRAW    IMPORTING AMOUNT TYPE DMBTR
                  EXPORTING NEW_BALANCE TYPE DMBTR
                  EXCEPTIONS INSUFFICIENT_FUNDS,
      DISPLAY     EXPORTING NEW_BALANCE TYPE DMBTR.
ENDCLASS.

CLASS lcl_account IMPLEMENTATION.
    METHOD SET_DEPOSIT.
      BALANCE = NEW_BALANCE.
    ENDMETHOD.
    METHOD DEPOSIT.
      BALANCE = BALANCE + AMOUNT.
      NEW_BALANCE = BALANCE.
    ENDMETHOD.
    METHOD WITHDRAW.
      IF AMOUNT < BALANCE.
        BALANCE = BALANCE - AMOUNT.
        NEW_BALANCE = BALANCE.
      ELSE.
        RAISE INSUFFICIENT_FUNDS.
      ENDIF.
    ENDMETHOD.
    METHOD DISPLAY.
      WRITE: / 'BALANCE =', BALANCE.
    ENDMETHOD.
ENDCLASS.

DATA: main_account TYPE REF TO lcl_account.

START-OF-SELECTION.
  data: lv_excep  TYPE REF TO CX_ROOT,
  gv_message type string.

  CREATE OBJECT: main_account.

  CALL METHOD main_account->set_deposit
    EXPORTING
      NEW_BALANCE = 100.

  CALL METHOD main_account->deposit
    EXPORTING
      AMOUNT = 100.

  TRY.
    CALL METHOD main_account->withdraw
      EXPORTING
        AMOUNT = 5000.
  CATCH CX_ROOT INTO lv_excep.
    gv_message = lv_excep->get_text( ).
    WRITE :/ 'Exception = ', gv_message.
  ENDTRY.

  CALL METHOD main_account->display.

Former Member
Former Member replied

Hi,

Its like this.. When ever an Event has been raise there should be a handler for that event... So you have to

handle that event in your program as it is Local Class and also Local Event.

So you have to SET HANDLER for that Event...

Please check this sample code...

*Event Class Definition------------------------------------------------*
CLASS Event_class DEFINITION.
  PUBLIC SECTION.
    METHODS add_value.
    EVENTS  w_event EXPORTING value(w_val) TYPE i.
  PRIVATE SECTION.
    DATA: w_cntr     TYPE i,
          W_begn     TYPE i VALUE 10.
ENDCLASS.
*&Event Class Implementation-------------------------------------------*
CLASS Event_class IMPLEMENTATION.
  METHOD add_value.
    DATA diff TYPE i.
    ADD 1 TO w_cntr.
    IF w_cntr > w_begn.
      diff = w_cntr - w_begn.
      RAISE EVENT w_event EXPORTING w_val = diff.
    ENDIF.
  ENDMETHOD.
ENDCLASS.
*&Handler class Definition---------------------------------------------*
CLASS handler_class DEFINITION.
  PUBLIC SECTION.
    METHODS event_handler
            FOR EVENT w_event OF Event_class
            IMPORTING w_val.
ENDCLASS.
                   "handler DEFINITION
*Handler Class Implementation------------------------------------------*
CLASS handler_class IMPLEMENTATION.
  METHOD event_handler.
    WRITE: / 'Value is', w_val.
  ENDMETHOD.
ENDCLASS.
*Work Variables--------------------------------------------------------*
DATA: r1 TYPE REF TO Event_class,
      h1 TYPE REF TO handler_class.

START-OF-SELECTION.

  CREATE OBJECT: r1, h1.

  SET HANDLER h1->event_handler FOR r1.

  DO 20 TIMES.
    CALL METHOD r1->add_value.
  ENDDO.

Please check this thread and search of Events so that you would find one more example related to it

http://www.erpgenie.com/abap/OO/eg.htm

Hope this would help you.

Good luck

Narin

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question