Module 32: Error Handling & Logging
Error handling is not about avoiding failures — it is about failing safely, visibly, and traceably.
In enterprise SAP systems, every critical error must be logged and traceable.
Why Exceptions?
Exceptions provide:
- Clear error propagation
- Structured error handling
- Avoidance of sy-subrc checks
Exceptions are preferred over sy-subrc return codes.
Exception Types
Checked Exceptions
CLASS zcx_custom_error DEFINITION
INHERITING FROM cx_static_check.
ENDCLASS.
Unchecked Exceptions
CLASS zcx_runtime_error DEFINITION
INHERITING FROM cx_no_check.
ENDCLASS.
Use checked exceptions for recoverable errors, unchecked for critical failures.
1. Application Logs (SLG1)
What is SLG1?
SLG1 (Application Log) is SAP’s standard logging framework used to:
- Log errors, warnings, and info messages
- Persist logs across sessions
- Enable support & audit analysis
SLG1 is the default logging mechanism for background jobs and interfaces.
1.1 Log Structure
An application log consists of:
- Object (e.g.
Z_SALES) - Subobject (e.g.
UPLOAD) - Messages (E/W/I)
1.2 Creating an Application Log
DATA lv_log_handle TYPE balloghndl.
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_object = 'Z_SALES'
i_subobject = 'UPLOAD'
IMPORTING
e_log_handle = lv_log_handle.
1.3 Adding Messages to Log
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = lv_log_handle
i_msgty = 'E'
i_msgid = 'ZMSG'
i_msgno = '001'
i_msgv1 = 'Invalid amount'.
1.4 Saving the Log
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
i_log_handle = lv_log_handle.
Always save logs before COMMIT WORK.
2. Custom Logging Frameworks
Why Custom Logging?
Direct SLG1 function calls:
-
Are verbose
-
Lead to duplication
-
Are error-prone
Wrap SLG1 inside a logging utility class.
2.1 Logger Class Pattern
CLASS zcl_logger DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING iv_object TYPE balobj_d
iv_subobj TYPE balsubobj,
error
IMPORTING iv_text TYPE string,
info
IMPORTING iv_text TYPE string,
save.
PRIVATE SECTION.
DATA gv_log_handle TYPE balloghndl.
ENDCLASS.
2.2 Using the Logger
DATA(lo_logger) = NEW zcl_logger(
iv_object = 'Z_SALES'
iv_subobj = 'PROCESS'
).
lo_logger->error( 'Sales amount is invalid' ).
lo_logger->save( ).
Consistent logging across the system.
3. Exception Propagation Strategies
3.1 Class-Based Exceptions (Modern ABAP)
Use class-based exceptions instead of return codes.
RAISE EXCEPTION TYPE zcx_sales_error
EXPORTING
textid = zcx_sales_error=>invalid_amount.
Exceptions should represent exceptional situations, not normal flow.
3.2 Propagate, Don’t Swallow
❌ Bad
TRY.
do_something( ).
CATCH cx_root.
" Ignore error
ENDTRY.
✅ Good
TRY.
do_something( ).
CATCH zcx_sales_error INTO DATA(lo_exc).
lo_logger->error( lo_exc->get_text( ) ).
RAISE.
ENDTRY.
3.3 Translate Exceptions at Boundaries
At architectural boundaries:
- Translate technical exceptions
- Into business exceptions or messages
CATCH cx_sql_exception INTO DATA(lo_sql).
RAISE EXCEPTION TYPE zcx_business_error
EXPORTING previous = lo_sql.
-
DB layer → technical exception
-
Service layer → business exception
-
UI layer → message
4. Error Handling by Context
Dialog Programs
- Raise exception
- Show MESSAGE
- Log if needed
Background Jobs
-
Log error (SLG1)
-
Do NOT dump
-
Finish gracefully
OData / RAP
-
Raise framework-compatible messages
-
Let framework convert to HTTP errors
RAP automatically maps exceptions to OData responses.
5. Common Mistakes
- Ignoring exceptions
- Using MESSAGE inside deep layers
- Overusing sy-subrc
- No logging in background jobs
- Logging everything as error
6. Interview-Grade Answer
Q: How do you handle errors in ABAP applications?
Answer:
I use class-based exceptions for error propagation, log critical errors using SLG1 or a logging framework, translate exceptions at architectural boundaries, and ensure background jobs and services fail gracefully with traceable logs.
Shows production maturity and Clean ABAP thinking.
7. Summary
- SLG1 is SAP’s standard logging framework
- Custom loggers improve consistency
- Class-based exceptions are mandatory
- Exceptions must propagate correctly
- Logging + exception handling go together
8. Practice Exercises
- Create an SLG1 application log.
- Build a reusable logger class.
- Raise and catch custom exceptions.
- Translate technical to business exceptions.
- Implement logging in a background job.
9. What’s Next?
➡️ Module 33: Migration & Modernization
Errors are inevitable — untraceable errors are unacceptable.