09-26-2008 9:28 AM
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.
09-26-2008 9:39 AM
Hi Wong,
Please check in debugging and tell exactly at what place you are getting dump and what is the Error you see in dump.
You had Raised the exception and its fine... Where is the Handler for that exception.?
Regards
Narin
09-26-2008 9:39 AM
Hi Wong,
Please check in debugging and tell exactly at what place you are getting dump and what is the Error you see in dump.
You had Raised the exception and its fine... Where is the Handler for that exception.?
Regards
Narin
09-26-2008 9:47 AM
Hi Narin,
This is what I get when I executed the program.
Short text
Exception condition "INSUFFICIENT_FUNDS" raised.
What happened?
The current ABAP/4 program encountered an unexpected
situation.
Error analysis
A RAISE statement in the program "!ZOOP_ACCOUNT" raised the exception
condition "INSUFFICIENT_FUNDS".
Since the exception was not intercepted by a superior
program, processing was terminated.
Short description of exception condition:
For detailed documentation of the exception condition, use
Transaction SE37 (Function Library). You can take the called
function module from the display of active calls.
09-26-2008 10:01 AM
Hi,
Please check whether INSUFFICIENT_FUNDS exception was handled or not.. I doubt its because of improper handling of that exception.
Good luck
Narin
09-26-2008 10:17 AM
09-26-2008 10:29 AM
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
09-26-2008 11:11 AM
Hi Narin,
Thanks for your kind help. The example you posted here are really useful to me.
It is working perfectly for me now.
09-26-2008 11:16 AM
HI myahsam wong ,
i am also face the same problem .can you copy the code plz?
Thanks,
Mahesh.
09-27-2008 1:43 AM
Hi mahesh,
You can have a look into the code I have amended.
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.
EVENTS INSUFFICIENT_FUNDS.
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.
WRITE: / 'BALANCE =', BALANCE.
ELSE.
RAISE EVENT INSUFFICIENT_FUNDS.
ENDIF.
ENDMETHOD.
* METHOD DISPLAY.
* WRITE: / 'BALANCE =', BALANCE.
* ENDMETHOD.
ENDCLASS.
CLASS handler_class DEFINITION.
PUBLIC SECTION.
METHODS event_handler
FOR EVENT INSUFFICIENT_FUNDS OF lcl_account.
ENDCLASS.
"handler DEFINITION
*Handler Class Implementation------------------------------------------*
CLASS handler_class IMPLEMENTATION.
METHOD event_handler.
WRITE: / 'Value withdraw Exceeded'.
ENDMETHOD.
ENDCLASS.
DATA: main_account TYPE REF TO lcl_account,
h1 TYPE REF TO handler_class.
PARAMETERS: P1 TYPE DMBTR,
P2 TYPE DMBTR,
P3 TYPE DMBTR.
START-OF-SELECTION.
CREATE OBJECT: main_account, h1.
SET HANDLER h1->event_handler FOR main_account.
CALL METHOD main_account->set_deposit
EXPORTING
NEW_BALANCE = P1.
CALL METHOD main_account->deposit
EXPORTING
AMOUNT = P2.
CALL METHOD main_account->withdraw
EXPORTING
AMOUNT = P3.
09-29-2008 4:56 AM