Handling Exceptions During RFCs
The example demonstrates the different ways of handling errors during an RFC.
DATA demo_exception TYPE REF TO cx_demo_exception. DATA msg TYPE c LENGTH 255. "Classical exception handling CALL FUNCTION 'DEMO_RFM_CLASSIC_EXCEPTION' DESTINATION 'NONE' EXCEPTIONS system_failure = 1 MESSAGE msg communication_failure = 2 MESSAGE msg OTHERS = 3. CASE sy-subrc. WHEN 1. WRITE: / 'EXCEPTION SYSTEM_FAILURE', msg. WHEN 2. WRITE: / 'EXCEPTION COMMUNICATION_FAILURE', msg. WHEN 3. WRITE: / 'EXCEPTION OTHERS'. ENDCASE.
Function module DEMO_RFM_CLASS_BASED_EXCEPTION triggers the class-based exception CX_DEMO_EXCEPTION. Function module DEMO_RFM_CLASSIC_EXCEPTION triggers the non-class-based exception CLASSIC_EXCEPTION.
EXCEPTIONS is not specified during the first two calls, which means that class-based exceptions are handled:
- When DEMO_RFM_CLASS_BASED_EXCEPTION is called, any possible class-based exceptions are handled with
CATCH. If none of the predefined exceptions of the RFC interface are raised, CX_DEMO_EXCEPTION is caught.
- Possible class-based exceptions are also handled with
CATCHwhen DEMO_RFM_CLASSIC_EXCEPTION is called. If none of the predefined exceptions of the RFC interface are raised, CX_CLASSIC_EXCEPTION is caught whose attribute EXCEPTION_NAME contains the name of the original classic exception.
EXCEPTIONS is specified during the other two calls, which means that non-class-based exceptions are handled:
- When DEMO_RFM_CLASS_BASED_EXCEPTION is called, predefined exception SYSTEM_FAILURE of the RFC interface is raised because class-based exception CX_DEMO_EXCEPTION cannot be handled.
- When DEMO_RFM_CLASSIC_EXCEPTION is called, possible classic exceptions are assigned values for
sy-subrc. If none of the predefined exceptions of the RFC interface are raised,
sy-subrcis set to "4".