‎2011 Feb 16 2:39 AM
Hi,
I have a class and the class has two methods, setdata and getdata.
The setdata method receives 2 input strings and updates a Z table.
Getdata outputs the data from the same Z table.
Now i want to handle run time errors.
so created the exception class and its super class is cx_static_check.
i have defined a method in exception class, it will print "parameters are blank", if the passing parameter to setdata does not contain any value.
in the method setdata i have used inside a if endif..try..catch endtry. statement.
Try checks whether the values are blank, if blank it raise exception and that needs to be caught by the catch statement into an object reference. using the object refernce i call method and print statement "parameters are blank".
now i get error "no open try statement".but in the setdata method i have both try...endtry.
also i have a doubt regarding choosing the check box "class based exception" for a method.
Does the methos that handles exception should be checked "class based exception" or which method to be checked as "class based exception"?
‎2011 Feb 16 8:43 AM
To make this work you need:
1) In exception class i.e. ZCX_CLA_TEST on Text tab add NO_PARAMETERS with same description
2) In method's signature check the Exception Classes checkbox and give your exception class name there
3) In method itself raise an exception with this textid
METHOD set_data.
if i_param is not supplied or
i_param is initial.
"raise exception
RAISE EXCEPTION TYPE zcx_cla_test
EXPORTING textid = zcx_cla_test=>no_parameters.
endif.
ENDMETHOD.
4) In client program catch this exception and get its message
DATA gr_exc TYPE REF TO zcx_cla_test.
DATA g_mess TYPE string.
TRY .
z_cla_test=>set_data( ).
CATCH zcx_cla_test INTO gr_exc.
g_mess = gr_exc->get_text( ).
MESSAGE g_mess TYPE 'I'.
ENDTRY.
Regards
Marcin
‎2011 Feb 17 5:01 AM
Hi,
you have mentioned "In method's signature check the Exception Classes checkbox and give your exception class name there"..
So i have a exception class ZCX_A and there is a method NullHandler to handle the exception..In setdata method, i check for the initial values and if they are initial then i will raise exception type ZCX_A. It will be caught by catch ZCX_A into object. Then i would use call method object->NullHandler. Now where should i enable the the Exception Classes checkbox? Whether in the method NullHandler of class ZCX_A or in the setdata method which actually raise the exception?
‎2011 Feb 17 7:46 AM
You always have to list all exceptions that method can raise. So you need that in set_data method. Only then you are able to raise an exception within its body.
This is the same as you would be using classical error in Function Module. When you call any FM which have the exception listed you get something like
CALL FUNCTION ...
IMPORTING ....
EXCEPTIONS
something_wrong = 1
something_other_wrong = 2
others = 3.
See, here all exceptions are listed which FM can raise inside its body. The same situation is with methods and class based exceptions, but you embrace its call within TRY.,.CATCH block instead.
Regards
Marcin
‎2011 Feb 21 2:42 AM