Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

class based exception

lakshminarasimhan_n4
Active Contributor
0 Likes
1,305

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"?

4 REPLIES 4
Read only

MarcinPciak
Active Contributor
0 Likes
747

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

Read only

0 Likes
747

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?

Read only

0 Likes
747

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

Read only

lakshminarasimhan_n4
Active Contributor
0 Likes
747

Answered