You are on page 1of 20

Exception Handling,

Logging and Tracing


By Ahmed DEBIBI
June, 2014
1
Plan
Overview
Exceptions Handling
Error types
How to manage errors correctly
Logging and Tracing
Overview

2
Overview
The objective of exception framework and logging and
tracing is to improve the quality of our applications.
Exception framework intends to improve error and exception handling,
and especially avoid SAP users having a dump when there is an
unexpected situation.

Logging API provides a service that allows the developers to log a
debug, info, warning or error messages. It also allows developers to log
an exception. Activating and Browsing the Logs are available in the
Setup Administration area.

EXCEPTIONS HANDLING
4
Error that you know how to porcess
directly
Process the error directly; you dont need to report it to user.
Example: you read a record and you got an error that it does not exists; in that case
and if it matches with your scenario, trap the error and if the error occurs you can
create a new record. You dont need to report the error to user.
5
Error that you cannot process at
your layer level ? (1/3)
The error needs to go up through layers, be processed when it is possible or
displayed to user.
Example: you log on to a system via an API; you got an error Invalid password. This
error should go up to UI layer, and then be displayed or ensure that a new password
is prompted.
Independently from the situation, a way for an error to reach the UI Layer must exist,
because even if at one point in time, the scenario does not exist, you are not sure
than in the future you will have to modify your code and be in the case where you
have an error that has to be reported to UI.

6
Error that you cannot process at
your layer level ? (2/3)
In order to create this pass-through:

7
Error that you cannot process at
your layer level ? (3/3)
All methods implemented need to include the CX_SISE_EXCEPTION in its
signature. In addition this exception must be declared as Resumable.




It means that if an exception is raised in a lower layer, if the exception is not
caught by an intermediate layer it will pass through to reach finally the UI Layer
where it MUST be caught. Once caught at UI layer, the error is processed or
displayed to user via a utility class that will provide a set of methods to report
error to user according to the client UI technology. This class is the exception
reporter.





8
HOW TO MANAGE ERRORS
CORRECTLY
9
How to manage errors correctly
As mentioned previously, all errors must be converted to
exception CX_SISE_EXCEPTION. In ABAP it exists various
way to detect errors being exception, sy-subrc, message tables,
and etc.
All kinds of errors must be converted to
CX_SISE_EXCEPTION. The exception class
CX_SISE_EXCEPTION offers a set of methods to help in
converting errors.
10
Report a new error
You are in the case, where you have to report a
brand new error:
Create a message,
Generate the exception from SY,
and raise it.

11
data lx_exception type ref to cx_sise_exception.
data l_message type string.
message e001(ags_sise_error) with SY-UNAME into l_message.
lx_exception = cx_sise_exception=>create_from_sy( ).
raise exception lx_exception.
Convert an existing exception
You are in the case, where you called an API that raise an exception that is
not CX_SISE_EXCEPTION. Catch the exception, and create the
CX_SISE_EXCEPTION from the caught exception.

12
data lx_exception type ref to cx_sise_exception.
data lx_ags_smt_solar_twb type ref to cx_ags_smt_solar_twb.
data l_user_name type xubname.
l_user_name = 'TOTO'.
try.
cl_ags_smt_twb_user_cu=>exists( l_user_name ).
catch cx_ags_smt_solar_twb into lx_ags_smt_solar_twb.
lx_exception =cx_sise_exception=>create_from_exception(lx_ags_smt_solar_twb ).
raise exception lx_exception.
endtry.
Raise an exception linked with the
previous one
You are in the case, where you called an API that raise an exception and you
want keep that exception as is, but be able to report it with the standard
mechanism. Catch the exception, and raise a cx_sise_exception exporting
the previous exception.

13
data lx_exception type ref to cx_sise_exception.
data lx_ags_smt_solar_twb type ref to cx_ags_smt_solar_twb.
data l_user_name type xubname.
l_user_name = 'TOTO'.
try.
cl_ags_smt_twb_user_cu=>exists( l_user_name ).
catch cx_ags_smt_solar_twb into lx_ags_smt_solar_twb.
raise exception type cx_sise_exception exporting previous = lx_ags_smt_solar_twb.
endtry.
Create exception from a sy-subrc
You are in the case, where you called an API that returns a SY-SUBRC and
you have to generate an exception from that sy-subrc. Create a message
explaining the error , then create the exception from SY. (note that in the
example below , it tests only if sy-subrc is different of 0 . But normally we
should have a CASE WHEN WHENENDCASE for each sy-subrc
possible values and an adhoc message.
14
data lx_exception type ref to cx_sise_exception.
data l_carrid type s_carr_id.
data l_msg type string.
select single carrid from sflight into l_carrid where carrid = '999'.
if sy-subrc <> 0.
message i000(ags_sise_error) into l_msg.
lx_exception = cx_sise_exception=>create_from_sy( ).
raise exception lx_exception.
endif.

Create exception from a Class
Message
You are in the case where you have to generate an exception
with a class message and message number. Create a message
explaining the error in a class message, then create the
exception from it.
15
data lx_exception type ref to cx_sise_exception.
" Create the exception
lx_exception = cx_sise_exception=>create_from_class_message(
i_message_class_name = 'CL_GPA_MESSAGE' " Class Message
i_message_number = '035' " Message Number
io_logger = cl_sise_lt=>get_instance( )
).
raise exception lx_exception.
Create exception from BAPIRET
Table
You are in the case, where you called an API that returns error
messages in a BAPIRET Table. Create the exception from the
BAPIRET Table.
DATA: lx_exception TYPE REF TO cx_sise_exception,
lt_bapiret TYPE bapirettab,
ls_object_id TYPE etapi_obj.
CALL FUNCTION 'ECATT_OBJ_PAR_GET'
EXPORTING
is_key = ls_object_id
" IMPORTING
" et_params = et_parameter
TABLES
return = lt_bapiret.
IF lt_bapiret IS NOT INITIAL.
lx_exception = cx_sise_exception=>create_from_bapirettab( lt_bapiret ).
RAISE EXCEPTION lx_exception.
ENDIF.
16
Create a resumable exception
You are in the case, where you need to raise an exception but you want let
the possibility to let the process continue. In that case you have to raise a
resumable exception. Once the exception will be catch the catcher will be
able to decide if he wants or not to continue the process.
WARNING: In order that resumable exception works it is very important that
all exception in the entire way through the layers be flagged as
Resumable in methods signatures
data lx_exception type ref to cx_sise_exception.
data l_message type string.
message e002(ags_sise_error) into l_message.
lx_exception = cx_sise_exception=>create_from_sy( ).

RAISE RESUMABLE EXCEPTION lx_exception.

*. Continue process here
17
A generic way to catch and handle
Exception at UI Level
To handle CX_SISE_EXCEPTION you have to catch it. If you do that at UI Layer level, you
should most of the time report the error to end user. The class Cl_sise_exception_reporter will
propose a set of method to make reporting easier. For instance, to display the error message in
to the message area of webdynpro , you can use method report_error_to_message_area.
In order to manage resumable exception, you have to use BEFORE UNWIND keywords in
the catch statements.
If the exception you receive is resumable the flag is_resumable of the exception will be set.
Then it is up to you to decide if you want resume or not according to your functional need. To
resume simply call resume. When resume is called, the process located just after where the
exception has been triggered will continue.
data lx_exception type ref to cx_sise_exception.
try.
cl_sise_business_layer=>call_a_method( ).
catch BEFORE UNWIND cx_sise_exception into lx_exception.
cl_sise_exception_reporter=>report_error_to_message_area(
io_message_manager = wd_this->mo_message_manager ix_agssise_exception = lx_exception ).
if lx_exception->is_resumable = abap_true.
resume.
endif.
endtry.
18
LOGGING AND TRACING
19
Overview
The Logging API provides a service that allows the developers
to log a debug, info, warning or error messages.
It also allows you to log an exception.
Activating and Browsing the Logs are available in the Setup
Administration area. (transaction: solman_setup_admin)
20

You might also like