We will start with one of the powerful features which is already "old" i.e. it's been there in the system surely for more than a decade but I do not see many developers using it at all - it is the Resumable Exception. I will demonstrate the handling of a same business scenario exception with and without its Resumable option to understand the difference and also the benefits of a modern development design which leverages these powerful features ultimately saving time & effort which equals money to the customer in the long run.
Report ztest.
CLASS lcl_employee DEFINITION FINAL CREATE PUBLIC.
PUBLIC SECTION.
TYPES: BEGIN OF empl_data,
empid TYPE int4, "Employee ID
emptyp TYPE string, "Org Assignment data
salary TYPE decfloat16, "Pay data
phone TYPE numc10, "Communication data
END OF empl_data,
empl_data_t TYPE SORTED TABLE OF empl_data WITH UNIQUE KEY empid.
METHODS constructor IMPORTING VALUE(i_empid) TYPE int4.
METHODS get_data RETURNING VALUE(rs_result) TYPE empl_data
RAISING RESUMABLE(cx_no_data_found).
PRIVATE SECTION.
DATA emp_id TYPE int4.
METHODS get_emptyp RETURNING VALUE(r_result) TYPE string
RAISING cx_no_data_found.
METHODS get_salary RETURNING VALUE(r_result) TYPE decfloat16
RAISING RESUMABLE(cx_no_data_found).
METHODS get_phone RETURNING VALUE(r_result) TYPE numc10.
METHODS get_emp_id RETURNING VALUE(r_result) TYPE int4.
ENDCLASS.
CLASS lcl_employee IMPLEMENTATION.
METHOD constructor.
me->emp_id = i_empid.
ENDMETHOD.
METHOD get_data.
rs_result = VALUE #( empid = me->get_emp_id( )
emptyp = me->get_emptyp( )
salary = me->get_salary( )
phone = me->get_phone( )
).
ENDMETHOD.
METHOD get_emptyp.
r_result = SWITCH #( me->get_emp_id( )
WHEN 1 THEN |Full-Time|
WHEN 2 THEN |Part-Time|
WHEN 3 THEN |Contractor|
WHEN 4 THEN |Casual|
ELSE THROW cx_no_data_found(
rel_proc_id = CONV #( me->get_emp_id( ) ) )
).
ENDMETHOD.
METHOD get_phone.
r_result = SWITCH #( me->get_emptyp( )
WHEN `Full-Time` THEN |1234567890|
WHEN `Part-Time` THEN |5678901234|
WHEN `Casual` THEN |7890123456|
ELSE |0399999999|
).
ENDMETHOD.
METHOD get_salary.
r_result = SWITCH #( me->get_emptyp( )
WHEN `Full-Time` THEN 50000
WHEN `Part-Time` THEN 25000
WHEN `Casual` THEN 5000
ELSE THROW RESUMABLE cx_no_data_found(
rel_proc_id = CONV #( me->get_emp_id( ) ) )
).
ENDMETHOD.
METHOD get_emp_id.
r_result = me->emp_id.
ENDMETHOD.
ENDCLASS.
DATA extract_t TYPE lcl_employee=>empl_data_t.
DATA error_t TYPE string_table.
START-OF-SELECTION.
DATA(all_employees_t) = VALUE int4_table( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ).
LOOP AT all_employees_t REFERENCE INTO DATA(dref).
TRY.
INSERT NEW lcl_employee( dref->* )->get_data( ) INTO TABLE extract_t.
CATCH BEFORE UNWIND cx_no_data_found INTO DATA(lx_no_data).
IF lx_no_data->is_resumable = abap_true.
"Resumable Exception was raised
RESUME.
ELSE.
"Non-Resumable Exception was raised
error_t = VALUE #( BASE error_t ( lx_no_data->get_text( ) ) ).
ENDIF.
ENDTRY.
ENDLOOP.
cl_demo_output=>new( )->write( extract_t )->write( error_t )->display( ).
CLASS lcl_employee DEFINITION FINAL CREATE PUBLIC.
PUBLIC SECTION.
TYPES: BEGIN OF empl_data,
empid TYPE int4, "Employee ID
emptyp TYPE string, "Org Assignment data
salary TYPE decfloat16, "Pay data
phone TYPE numc10, "Communication data
END OF empl_data,
empl_data_t TYPE SORTED TABLE OF empl_data WITH UNIQUE KEY empid.
METHODS constructor IMPORTING VALUE(i_empid) TYPE int4.
METHODS get_emptyp RETURNING VALUE(r_result) TYPE string
RAISING cx_no_data_found.
METHODS get_salary RETURNING VALUE(r_result) TYPE decfloat16
RAISING cx_no_data_found.
METHODS get_phone RETURNING VALUE(r_result) TYPE numc10.
METHODS get_emp_id RETURNING VALUE(r_result) TYPE int4.
PRIVATE SECTION.
DATA emp_id TYPE int4.
ENDCLASS.
CLASS lcl_employee IMPLEMENTATION.
METHOD constructor.
me->emp_id = i_empid.
ENDMETHOD.
METHOD get_emptyp.
r_result = SWITCH #( me->get_emp_id( )
WHEN 1 THEN |Full-Time|
WHEN 2 THEN |Part-Time|
WHEN 3 THEN |Contractor|
WHEN 4 THEN |Casual|
ELSE THROW cx_no_data_found(
rel_proc_id = CONV #( me->get_emp_id( ) ) )
).
ENDMETHOD.
METHOD get_phone.
r_result = SWITCH #( me->get_emptyp( )
WHEN `Full-Time` THEN |1234567890|
WHEN `Part-Time` THEN |5678901234|
WHEN `Casual` THEN |7890123456|
ELSE |0399999999|
).
ENDMETHOD.
METHOD get_salary.
r_result = SWITCH #( me->get_emptyp( )
WHEN `Full-Time` THEN 50000
WHEN `Part-Time` THEN 25000
WHEN `Casual` THEN 5000
ELSE THROW cx_no_data_found(
rel_proc_id = CONV #( me->get_emp_id( ) ) )
).
ENDMETHOD.
METHOD get_emp_id.
r_result = me->emp_id.
ENDMETHOD.
ENDCLASS.
Report ztest.
DATA extract_t TYPE lcl_employee=>empl_data_t.
DATA error_t TYPE string_table.
START-OF-SELECTION.
DATA(all_employees_t) = VALUE int4_table( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ).
LOOP AT all_employees_t REFERENCE INTO DATA(dref).
DATA(ref) = NEW lcl_employee( dref->* ).
TRY.
DATA(extract) = VALUE lcl_employee=>empl_data(
empid = ref->get_emp_id( )
emptyp = ref->get_emptyp( )
phone = ref->get_phone( )
).
CATCH cx_no_data_found INTO DATA(lx_no_data).
error_t = VALUE #( BASE error_t ( lx_no_data->get_text( ) ) ).
CONTINUE.
ENDTRY.
"Note that the call to the method GET_SALARY( ) is separated
TRY.
extract = VALUE #( BASE extract
salary = ref->get_salary( )
).
CATCH cx_no_data_found INTO lx_no_data.
ENDTRY.
INSERT extract INTO TABLE extract_t.
ENDLOOP.
cl_demo_output=>new( )->write( extract_t )->write( error_t )->display( ).
Report ztest_new.
DATA extract_t TYPE lcl_employee=>empl_data_t.
DATA error_t TYPE string_table.
START-OF-SELECTION.
DATA(all_employees_t) = VALUE int4_table( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ).
LOOP AT all_employees_t REFERENCE INTO DATA(dref).
TRY.
INSERT NEW lcl_employee( dref->* )->get_data( ) INTO TABLE extract_t.
"This time around it was not handled as a Resumable Exception
CATCH cx_no_data_found INTO DATA(lx_no_data). "<--Handled normally
error_t = VALUE #( BASE error_t ( lx_no_data->get_text( ) ) ).
ENDTRY.
ENDLOOP.
cl_demo_output=>new( )->write( extract_t )->write( error_t )->display( ).
Report ztest_new.
DATA extract_t TYPE lcl_employee=>empl_data_t.
DATA error_t TYPE string_table.
START-OF-SELECTION.
DATA(all_employees_t) = VALUE int4_table( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ).
LOOP AT all_employees_t REFERENCE INTO DATA(dref).
TRY.
INSERT VALUE #(
LET ref = NEW lcl_employee( dref->* ) IN
empid = ref->get_emp_id( )
emptyp = ref->get_emptyp( )
phone = ref->get_phone( )
salary = ref->get_salary( )
)
INTO TABLE extract_t.
CATCH cx_no_data_found INTO DATA(lx_no_data).
error_t = VALUE #( BASE error_t ( lx_no_data->get_text( ) ) ).
ENDTRY.
ENDLOOP.
cl_demo_output=>new( )->write( extract_t )->write( error_t )->display( ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
2 | |
2 |