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. The SAP Help Portal describes local classes as follows:
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).
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
.
As noted by matthew.billingham in the comment section, 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.
*"* 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.
"Program error in ALV-based output list, contact support
RAISE EXCEPTION TYPE lcx_ca_gen_report
MESSAGE e014(j_3rvatd).
Local exception classes can provide distinct exceptions to internal class methods without creating a new global exception class. However, do they have added value in a particular situation? What is your opinion? Do you consider them a useful option for specific cases or a bad habit for lazy developers? Good practice or bad practice?
Share your feedback in the comments 👇
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
4 | |
4 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 |