Application Development 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: 

Raise statement in Class method

Former Member
0 Kudos

Hi,

In my function module ztest I create the instance of the class and execute a public method of the class. Upon encountering a certain condition in the class method I want the program flow to stop and return back to the function module.

I try to raise an exception in the class method using .

raise ENDING_RUN.

But it gives a dump. How do I propagate control back to the function module from the class method when an if condition is met.

Thanks

7 REPLIES 7

huseyindereli
Active Contributor
0 Kudos

Hi Vighneswaran ,

Have you defined the exception from the exceptions tab ?

If you don't , you'll get a short dump as explained below.

If the exception is triggered in a method or function module whose caller does not assign a return value to the exception, a runtime error is then triggered whose short dump contains the name of the exception.

After you define exception , it will appear at the exceptions part of the FM pattern and sy-subrc set to related code.

CALL FUNCTION 'ZZ_TEST'
 EXCEPTIONS
   ENDING_RUN  = 1
   OTHERS        = 2
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

0 Kudos

Hi the dump occurs within the class method itself.

I call the classs method from with the function module ztest.

Inside ztest

I do a create object of the class object.

call object->class_method.----


>from this method calls are made to various private methods.In one of the private methods I raise an exception.(iit dumps thee)

I expect the program flow return back to the function module from which I am calling.But I a

I am unable to achieve this.

Dump analysis

condition "END_RUN".

Since the exception was not intercepted by a superior program

in the hierarchy, processing was terminated.

Short description of exception condition:

How do I intercept this exception within my function module .

Edited by: Vighneswaran CE on May 14, 2011 8:47 PM

0 Kudos

Hi ,

That dump means , you didn't catch the exception that was fired from somewhere in your code.

When you raise an exception inside a method or function , it appears as below.

Lets say , your method is any_method and you call it from a FM. You have to use;

EXCEPTIONS
   ENDING_RUN  = 1
   OTHERS        = 2.

part if you dont want a short dump. If method fires ENDING_RUN exception than sy-subrc will be set to 1.

CALL METHOD go_object->any_method
 IMPORTING 
  .....
EXPORTING
  ......
 EXCEPTIONS
   ENDING_RUN  = 1
   OTHERS        = 2.
IF sy-subrc EQ 1.
" ENDING_RUN fired.....
MESSAGE 'ENDING_RUN happened' TYPE 'E'.
ENDIF.

Hope this helps you.

Regards.

MarcinPciak
Active Contributor
0 Kudos

In addition to Hüseyin's anwser, you need to define all exceptions which method can raise in its signature . Go to method parameters -> choose button Exceptions -> type in all exceptions there which you forseen the method will raise. Now, as long as you catch these exceptions (as suggested above) you won't get dump anymore.

Of cource this applies to old exceptions . It is adviced that you should use class based exceptions instead, especially in OO context. From rasing and catching point of view for such exception the only things that change are:

- for raising: in methods signature where you typed your exceptions select chebox class exceptions

- for catching: in calling unit use try - catch block

Just refer the link I gave you and study a bit. It will become clear how you should use them.

Regards

Marcin

Former Member
0 Kudos

Hi

Hope you are propagating the exception to the caller using the raising addition when you declare the method like :

Method A

importing ..

exporting ..

raising ...

And the same should be caught in the calling program assigning the exception with a value as shown by Hüseyin.

Its best to use class based exceptions in OO context , easy to handle exceptions.

Hope this helps.

Regards,

Pawan

matt
Active Contributor
0 Kudos

I only use class based exceptions now. (Which Function Modules can also raise, and Forms). They're so much easier to handle, and more powerful.

matt

Former Member
0 Kudos

for this you have to define in class definition.i.e,

class A definition.

methods: m importing .......

exporting........

raising cx_root.

endclass.

and you have to create object, parameters and then,

try.

call method object->m

exporting

..........

importing

.........

catch cx_root

write:/ 'exception raised;'.

endtry.

i hope this will helpful to you.

by,

pavan kumar.G