Friday 9 July 2021

Local ABAP exception classes (inside global classes)

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

Why use local exception classes?

Although it is generally best practice to create global exception classes, they can seem a bit overkill for some applications. Think of PoCs, demo scenarios, BAdI implementations, etc. A handy but not so well-known option for these situations is to define a local exception class inside the global class. These allow internal exceptions (exceptions raised within the class itself) to be handled without the need for another global DDIC object. 

Local definitions, especially local classes, can be useful as auxiliary classes for the global class. In local classes you can encapsulate implementation details that should not be visible from the outside (they should not even occur in the private section).

Class editor: source-code based or ADT

Before you get coding, take into consideration that local exception classes do not play well with the classical Form-Based Class Builder. Use the Source Code-Based editor or Eclipse with ADT instead.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

Defining a local exception class

 
Source-code based Class Builder

To add a local exception class definition within a global class first open the global class in SE24 or SE80. Next, open the menu path Goto → Local Definitions/Implementations → Local Definitions/Implementations.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

Alternatively, you can click the toolbar button labeled ‘Local Definitions/Implementations’. You can now enter the logic for the local exception class.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

Eclipse with ADT

To add or edit a local class definition in the ABAP Development Tools in Eclipse, first open the global class. Do this via double-clicking the corresponding entry in the Project Explorer or via the ‘Open ABAP Development Object’ tool. Open the latter via its menu option Navigate → Open ABAP Development Object or its corresponding keyboard shortcut Ctrl + Shift + A.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

After opening the global class, define the local exception class in the tab ‘Class-relevant Local Types’. Next, open its local types section by clicking the tab labeled ‘Local Types’. You can now enter the definition and logic.

Usually, it’s fine to just have the local class definition in the ‘Local Types’ tab. However, this can cause problems in some cases. Prevent potential issues by declaring the class separately using ‘DEFINITION DEFERRED’ in the ‘Class-relevant Local Types’ tab.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

Example


The example below showcases a basic exception class for handling exceptions in an ALV reporting class.

*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
CLASS lcx_ca_gen_report DEFINITION INHERITING FROM cx_dynamic_check.
  PUBLIC SECTION.
    INTERFACES if_t100_dyn_msg.
    ALIASES msgty FOR if_t100_dyn_msg~msgty.
ENDCLASS.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides

We can now use this exception class in the internal methods of the global class.

"Program error in ALV-based output list, contact support
RAISE EXCEPTION TYPE lcx_ca_gen_report
    MESSAGE e014(j_3rvatd).

Source: sap.com

No comments:

Post a Comment