Skip to Content
avatar image
Former Member

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

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Mar 22, 2017 at 01:00 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 22, 2017 at 12:24 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 22, 2017 at 12:58 PM

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

    Add comment
    10|10000 characters needed 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!

    Add comment
    10|10000 characters needed characters exceeded