12-09-2011 7:27 PM
Hi Team,
I have created onc z exception class ..i have few doubts regarding raising exceptions ...
my code is like this..
PARAMETERS: myamount TYPE i.
DATA: mysavings TYPE i VALUE 5000,
plannedbalance TYPE i,
ex TYPE REF TO ZCX_DEMO_EXCEPTION_TEST,
mes TYPE string.
START-OF-SELECTION.
RAISE EXCEPTION TYPE ZCX_DEMO_EXCEPTION_TEST EXPORTING amnt = myamount
saving = mysavings.
TRY.
plannedbalance = mysavings - myamount.
IF plannedbalance < 0.
. ENDIF.
CATCH zCX_DEMO_EXCEPTION_TEST INTO ex.
mes = ex->get_text( ).
MESSAGE mes TYPE 'I'.
ENDTRY.
...........my doubt is why we cant abel to cach the exception ..even we i catched them in TRY and entry.....for above code system is giving warning message...
but if we keep raise and catch statement between try and entry ...then no issues....it is working fine.
Please calrify me..
<removed by moderator>
Regards
Ramaky
Edited by: Thomas Zloch on Dec 9, 2011 8:36 PM
12-10-2011 10:34 AM
Hi Ramky,
EXCEPTION can be handled using CATCH statement only when the EXCEPTION is raised between TRY .. ENDTRY. You already have no issues when doing that properly.
Varun
12-10-2011 10:58 AM
12-11-2011 2:45 PM
Halo Yramki,
You should ideally raise the exception inside the Method of a class . Then you should catch this exception either in another method or report.
That means
plannedbalance = mysavings - myamount.
should be done inside the method of Class say Check_balance
IF plannedbalance < 0.
RAISE EXCEPTION TYPE ZCX_DEMO_EXCEPTION_TEST EXPORTING amnt = myamount
saving = mysavings.
endif.
Now catching this exception inthe report.
create object l_object of the class.
try.
l_object->check_balance( ).
catch ZCX_DEMO_EXCEPTION_TEST into l_exception.
mes = ex->get_text( ).
MESSAGE mes TYPE 'I'.
endtry.
Regards
Arshad
12-12-2011 3:31 PM
Hi Team,
thanks so much....
it means raise and catch keywords we cant separate them...always keep them in try and endtry......
corect me if am doing wrong.
Regards,
Ramaky
12-13-2011 4:27 AM
Hi Ramaky,
I am Posting a Example Program of Using Raising Statement This Will Give Brief Idea Of how to use Raise Statement And Try and Catch also.
CLASS ABC DEFINITION.
PUBLIC SECTION.
METHODS M1 IMPORTING X TYPE I
Y TYPE I
EXPORTING Z TYPE I
RAISING CX_SY_ZERODIVIDE.
ENDCLASS.
CLASS ABC IMPLEMENTATION.
METHOD M1.
Z = X / Y.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA OB TYPE REF TO ABC.
CREATE OBJECT OB.
PARAMETERS: P_X TYPE I,
P_Y TYPE I.
DATA LV_R TYPE I.
TRY.
CALL METHOD OB->M1
EXPORTING
X = P_X
Y = P_Y
IMPORTING
Z = LV_R.
CATCH CX_SY_ZERODIVIDE.
WRITE:/ 'EXCEPTION RAISED:'.
ENDTRY.
WRITE:/ 'DIVISION IS...',LV_R.
WRITE:/ 'END OF PROGRAM.......'.
This Will Definitely HelpFul To you.
Warm Regards,
PavanKumar.G
12-21-2011 3:03 PM
hI,
Thanks so much for your inputs...i got some useful point.
Regards
Rama
12-21-2011 3:56 PM
Whenever a exception is triggered, an exception object is created. This object would contain more information on the error condition. This exception can be caught and handled using the TRY .. CATCH.. ENDTRY block.
Read more - [Class-based Exceptions I u2013 Basics|http://help-abap.zevolving.com/2011/12/class-based-exception/]
Regards,
Naimesh Patel