Skip to Content
0

try. perform routine. catch exception type zcx results in uncaught exception

Mar 22, 2017 at 11:23 AM

161

avatar image
Former Member

We have a function module with a dozen subroutines which all have the possibility to raise an error to stop processing. It is too cumbersome to define an exception for every one of these cases so we have been updating a flag to tell subsequent routines to discontinue processing.

I had hoped that I could use

raise exception type zcx_error

But, if you raise that in a subroutine, SAP short dumps with "uncaught exception"!!!

Our code is like this:

function z_function.

data: lcx_error type ref to zcx_error.

try.
perform routine1.
perform routine2.

catch exception zcx_error into lcx_error.
endtry.

form routine1.
...
raise exception type zcx_error.
endform.

In other languages, a try...catch...endtry will always catch the exception and a RAISE will always go back to the last TRY statement to look for a CATCH. But, ABAP doesn't seem to?

The obvious path is to fully embrace OOP but that is currently too much code to rewrite, so I'm looking for suggestions.

Thanks in advance.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Matthew Billingham
Mar 22, 2017 at 01:00 PM
2

First of all, yes, you should go object oriented. Or stop using forms and use function modules instead. Forms are obsolete. Refactoring has an upfront cost, but should save a fortune down the line.

Second, catch exception zcx_error into lcx_error. is not valid abap. (Typo, I guess).

Third, you've not defined the forms as raising zcx_error.

As expected, if instead of zcx_error you used a an exception derived from CX_NO_CHECK (rather than one subclassed from cx_static_check), you don't get the dump.

Share
10 |10000 characters needed characters left characters exceeded
Suhas Saha
Mar 22, 2017 at 12:24 PM
2

In other languages, a try...catch...endtry will always catch the exception and a RAISE will always go back to the last TRY statement to look for a CATCH. But, ABAP doesn't seem to?

Will the CATCH handle the exception even if it is not declared in the signature of the procedure?

You are getting the runtime exception because the exception you raised inside the subroutine has not been propagated to the caller.

form routine1.

...raise exception type zcx_error.

endform.

You missed the RAISING addition in the signature of the subroutine & the exception is not propagated.

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Mar 22, 2017 at 12:58 PM
1

https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenexceptions_procedures.htm

"If a class-based exception is not handled in a procedure, the system attempts to propagate it to the caller of the procedure. The exceptions that can be propagated from a procedure must be declared in its interface."

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Mar 22, 2017 at 01:35 PM
-1

Wow. Just Wow.

I had no idea you could declare an exception in the subroutine FORM definition. Thank you everyone.

In the end, I changed my exception class to have a superclass of CX_NO_CHECK and my code all works fine now. Matthew suggested this and it works perfectly now. Best of all, I don't have to go back and edit all of our subroutines.

With the extra time, maybe we can start converting our function modules into classes/objects!

Show 2 Share
10 |10000 characters needed characters left characters exceeded

"I changed my exception class to have a superclass of CX_NO_CHECK"

I did not suggest that you should do this, I merely mentioned it as an aside. To be very clear I really do not think what you've done is correct. Saving time on the how long it takes you to program at the expense of the clarity and robustness of your code is not good practice. If you were working for me, I'd reject the code at code review. Seriously.

1
1