-
Notifications
You must be signed in to change notification settings - Fork 286
/
zcl_demo_abap_error_handling.clas.abap
1082 lines (897 loc) · 42.1 KB
/
zcl_demo_abap_error_handling.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"! <p class="shorttext"><strong>Exceptions and Runtime Errors</strong><br/>ABAP cheat sheet example class</p>
"!
"! <p>The example class demonstrates syntax and concepts related to exceptions and runtime errors.<br/>
"! Choose F9 in ADT to run the class.</p>
"!
"! <h2>Information</h2>
"! <p>Find information on getting started with the example class and the disclaimer in
"! the ABAP Doc comment of class {@link zcl_demo_abap_aux}.</p>
CLASS zcl_demo_abap_error_handling DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
CLASS-METHODS meth_a
IMPORTING num TYPE i
RAISING zcx_demo_abap_error_a.
CLASS-METHODS meth_b
IMPORTING num TYPE i
RAISING zcx_demo_abap_error_b.
CLASS-METHODS meth_resumable
IMPORTING num1 TYPE i
num2 TYPE i
RETURNING VALUE(div_result) TYPE string
RAISING RESUMABLE(zcx_demo_abap_error_b).
CLASS-METHODS divide
IMPORTING num1 TYPE i
num2 TYPE i
RETURNING VALUE(div_result) TYPE decfloat34
RAISING cx_sy_zerodivide.
CLASS-METHODS get_uuid
RETURNING VALUE(uuid) TYPE sysuuid_x16
RAISING cx_uuid_error.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_demo_abap_error_handling IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( |ABAP cheat sheet example: Exceptions and Runtime Errors\n\n| ).
out->write( `1) Exception Categories` ).
"Method specifying an exception class that inherits from CX_DYNAMIC_CHECK
"No explicit exception handling required
"The following statement does not show a syntax warning.
DATA(div_result1) = divide( num1 = 5 num2 = 2 ).
out->write( data = div_result1 name = `div_result1` ).
TRY.
DATA(div_result2) = divide( num1 = 5 num2 = 0 ).
CATCH cx_sy_zerodivide.
out->write( `Exception caught` ).
ENDTRY.
"Method specifying an exception class that inherits from CX_STATIC_CHECK
"Explicit exception handling required
"The following statement (commented in) shows a syntax warning.
"DATA(uuid1) = get_uuid( ).
TRY.
DATA(uuid2) = get_uuid( ).
CATCH cx_uuid_error.
ENDTRY.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `2) Raising Class-Based Exceptions` ) ).
"Note: The examples show a selection. More additions are available.
"Some are demonstrated further down.
"RAISE EXCEPTION statements
"RAISE EXCEPTION statement with the TYPE addition, specifying
"the name of a visible exception class; an exception
"object is created (if necessary, see the ABAP Keyword Documentation
"for more details)
TRY.
RAISE EXCEPTION TYPE cx_sy_zerodivide.
CATCH cx_sy_zerodivide.
ENDTRY.
"RAISE EXCEPTION statement specifying an exception object (an object
"reference variable pointing to an exception class)
DATA(exc) = NEW cx_sy_zerodivide( ).
TRY.
RAISE EXCEPTION exc.
CATCH cx_sy_zerodivide.
ENDTRY.
"Creating an exception object inline using the NEW operator
TRY.
RAISE EXCEPTION NEW cx_sy_zerodivide( ).
CATCH cx_sy_zerodivide.
ENDTRY.
"Note: Instances of abstract classes cannot be created. So, the
"following statements are not possible.
"RAISE EXCEPTION NEW cx_sy_arithmetic_error( ).
"RAISE EXCEPTION NEW cx_static_check( ).
"RAISE EXCEPTION NEW cx_dynamic_check( ).
"RAISE EXCEPTION NEW cx_no_check( ).
"RAISE EXCEPTION NEW cx_root( ).
"Dynamic creation of an exception object with CREATE OBJECT
DATA dyn_exc TYPE REF TO cx_root.
CREATE OBJECT dyn_exc TYPE ('CX_SY_ZERODIVIDE').
TRY.
RAISE EXCEPTION dyn_exc.
CATCH cx_root.
ENDTRY.
"COND/SWITCH operators with THROW addition
"THROW addition in conditional expressions with the COND and SWITCH operators
"enabling raising class-based exceptions in operand positions
"The addition works like RAISE EXCEPTION TYPE statements.
"COND operator
DATA(int1) = 1.
DATA(int2) = 0.
"The example considers ABAP "allowing" zero division when both operands are 0.
TRY.
DATA(res1) = COND decfloat34( WHEN ( int1 <> 0 AND int2 <> 0 ) OR ( int1 = 0 AND int2 <> 0 ) THEN int1 / int2
ELSE THROW cx_sy_zerodivide( ) ).
CATCH cx_sy_zerodivide.
ENDTRY.
"SWITCH operator
"The following example shows SWITCH with the THROW addition
"and uses cx_sy_zerodivide for demonstration purposes.
DO.
TRY.
DATA(num) = SWITCH #( sy-index
WHEN 1 THEN `one`
WHEN 2 THEN `two`
WHEN 3 THEN `three`
WHEN 4 THEN `four`
ELSE THROW cx_sy_zerodivide( ) ).
out->write( num ).
CATCH cx_sy_zerodivide.
out->write( `Exception caught` ).
EXIT.
ENDTRY.
ENDDO.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `3) Excursions with TRY control structures` ) ).
"TRY control structures are meant for handling catchable exceptions locally
"The example shows divisions. The predefined exception class cx_sy_zerodivide
"as suitable exception class is used.
"If the exception is not handled, the program is terminated and the runtime
"error COMPUTE_INT_ZERODIVIDE occurs.
"The third calculation is not carried out because the statement block is
"left due to the previous erroneous 0 division.
TRY.
DATA(div1) = 4 / 2.
out->write( data = div1 name = `div1` ).
out->write( |\n| ).
DATA(div2) = 4 / 0.
out->write( data = div2 name = `div2` ).
out->write( |\n| ).
DATA(div3) = 9 / 3.
out->write( data = div3 name = `div3` ).
out->write( |\n| ).
CATCH cx_sy_zerodivide.
out->write( `0 division. The exception was caught.` ).
out->write( |\n| ).
ENDTRY.
"It is possible to specify multiple exception classes in a list and
"multiple CATCH blocks.
"Note: If there are multiple CATCH blocks for exceptions that are in an inheritance
"relationship, you must pay attention that the more special exceptions are specified
"before the more general ones.
"The calculation example shows multiple CATCH blocks that themselves have more than
"one exception class specified. Here, local exception classes are specified just for
"demonstration purposes. They are not relevant in this TRY control structure.
DATA int_itab TYPE TABLE OF i WITH EMPTY KEY.
"Filling internal table of type i as basis for calculations
int_itab = VALUE #( ( 5 ) ( 0 ) ( 987654321 ) ).
LOOP AT int_itab ASSIGNING FIELD-SYMBOL(<fs_int>).
TRY.
out->write( |--- Calculations with { <fs_int> } ---| ).
DATA(calc1) = CONV decfloat34( 1 / <fs_int> ).
out->write( data = calc1 name = `calc1` ).
out->write( |\n| ).
DATA(calc2) = ipow( base = <fs_int> exp = 2 ).
out->write( data = calc2 name = `calc2` ).
out->write( |\n| ).
CATCH cx_sy_arithmetic_overflow .
out->write( `Arithmetic overflow. The exception was caught.` ).
CATCH cx_sy_zerodivide .
out->write( `0 division. The exception was caught.` ).
ENDTRY.
out->write( |\n| ).
ENDLOOP.
"The following example shows a catchable exception that is
"raised if a line is not found when using table expressions.
DATA(str_table) = VALUE string_table( ).
TRY.
DATA(line_tab) = str_table[ 12345 ].
"The predefined exception class cx_sy_itab_line_not_found
"as suitable exception class is used here.
"If the exception is not handled, the program is terminated
"and the runtime error ITAB_LINE_NOT_FOUND occurs.
CATCH cx_sy_itab_line_not_found.
out->write( `The line was not found. The exception was caught.` ).
ENDTRY.
"In the following CATCH block, the predefined exception class cx_sy_arithmetic_error
"is specified. Both cx_sy_zerodivide and cx_sy_arithmetic_overflow are derived from
"cx_sy_arithmetic_error which is an exception class higher up in the inheritance
"tree. Hence, cx_sy_arithmetic_error can be specified and handle both exceptions, too.
"The following example is basically the same as above. However, only one exception
"class is specified.
LOOP AT int_itab ASSIGNING FIELD-SYMBOL(<fs_int_inh>).
TRY.
out->write( |--- Calculations with { <fs_int_inh> } ---| ).
calc1 = 1 / <fs_int_inh>.
out->write( data = calc1 name = `calc1` ).
out->write( |\n| ).
calc2 = ipow( base = <fs_int_inh> exp = 2 ).
out->write( data = calc2 name = `calc2` ).
out->write( |\n| ).
CATCH cx_sy_arithmetic_error.
out->write( `Arithmetic error. The exception was caught.` ).
ENDTRY.
out->write( |\n| ).
ENDLOOP.
"Example demonstrating the exception root class cx_root specifying after CATCH to
"catch all catchable exception.
DO 3 TIMES.
TRY.
CASE sy-index.
WHEN 1.
RAISE EXCEPTION TYPE cx_sy_zerodivide.
WHEN 2.
RAISE EXCEPTION TYPE cx_uuid_error.
WHEN 3.
RAISE EXCEPTION TYPE cx_sy_itab_line_not_found.
ENDCASE.
CATCH cx_root.
"Instead of explicit specification of potential exception classes involved
"CATCH cx_sy_zerodivide cx_uuid_error cx_sy_itab_line_not_found.
ENDTRY.
ENDDO.
"Evaluating exception information (get_text method)
"You can use the addition INTO plus an object reference variable to store
"a reference to an exception object. It is, for example, relevant to
"determine the exact exception.
"The following example is the same as above using the more general exception
"class cx_sy_arithmetic_error. You can carry out certain tasks, for
"example, retrieving and displaying the exception text. To retrieve exception
"texts, you can call, for example, the method get_text.
DATA exception TYPE REF TO cx_root.
LOOP AT int_itab ASSIGNING FIELD-SYMBOL(<fs_int_into>).
TRY.
out->write( |--- Calculations with { <fs_int_into> } ---| ).
calc1 = 1 / <fs_int_into>.
out->write( data = calc1 name = `calc1` ).
out->write( |\n| ).
calc2 = ipow( base = <fs_int_into> exp = 2 ).
out->write( data = calc2 name = `calc2` ).
out->write( |\n| ).
CATCH cx_sy_arithmetic_error INTO exception.
"Note:
"- The object reference variable is of type cx_root.
"- You could also create the variable inline, e. g. ... INTO DATA(exc).
"Retrieving and displaying exception text
DATA(exception_text) = exception->get_text( ).
out->write( data = exception_text name = `exception_text` ).
ENDTRY.
out->write( |\n| ).
ENDLOOP.
"As above, the following example demonstrates an exception class for
"arithmetic operations that is higher up in the inheritance tree and
"catches zero division and arithmetic overflow errors.
DO 2 TIMES.
TRY.
IF sy-index = 1.
DATA(div) = CONV decfloat34( 1 / 0 ).
ELSE.
DATA(powers) = ipow( base = 10 exp = 100 ).
ENDIF.
CATCH cx_sy_arithmetic_error INTO DATA(error_arithm).
DATA(text) = error_arithm->get_text( ).
"Using RTTI to retrieve the relative name of the class
DATA(cl_name) = CAST cl_abap_classdescr(
cl_abap_typedescr=>describe_by_object_ref( error_arithm ) )->get_relative_name( ).
out->write( text ).
out->write( cl_name ).
ENDTRY.
ENDDO.
"Demonstrating the instance attribute 'previous' with
"nested TRY control structures.
"Consider the following scenario: A method is called and
"an exception is raised. Once evaluated, another exception
"is raised, and the previous exception (i.e. a reference to
"the previous exception object) is passed on to the caller.
"The caller can then evaluate the exceptions raised.
"The following example includes nested TRY control structures.
"In CATCH blocks, further exceptions are raised. There, the
"exception object is passed. In a WHILE loop, several pieces
"of information are retrieved, including the use of RTTI.
"Once the information has been retrieved, the previous attribute
"is used to assign the previous exception object. Note the cast.
DATA info TYPE string_table.
TRY.
TRY.
TRY.
RAISE EXCEPTION NEW cx_sy_zerodivide( ).
CATCH cx_sy_zerodivide INTO DATA(err1).
RAISE EXCEPTION NEW cx_sy_arithmetic_overflow( previous = err1 ).
ENDTRY.
CATCH cx_sy_arithmetic_overflow INTO DATA(err2).
RAISE EXCEPTION NEW cx_sy_itab_line_not_found( previous = err2 ).
ENDTRY.
CATCH cx_sy_itab_line_not_found INTO DATA(err3).
ENDTRY.
DATA(acc_err) = CAST cx_root( err3 ).
WHILE acc_err IS BOUND.
DATA(txt) = acc_err->get_text( ).
acc_err->get_source_position(
IMPORTING
program_name = DATA(prog_name)
include_name = DATA(incl_name)
source_line = DATA(src_line) ).
"Using RTTI to gather more information on the exception
"See the Dynamic Programming cheat sheet about RTTI.
DATA(tdo) = CAST cl_abap_classdescr( cl_abap_typedescr=>describe_by_object_ref( acc_err ) ).
DATA(cl_relative_name) = tdo->get_relative_name( ).
DATA(attributes) = tdo->attributes.
DATA(superclass) = tdo->get_super_class_type( )->get_relative_name( ).
"Populating the info table
APPEND |--------------- { sy-index } ---------------| TO info.
APPEND |Text: { txt }| TO info.
APPEND |Position of raised exception: { prog_name } / { incl_name } / { src_line }| TO info.
APPEND |Exception class name: { cl_relative_name }| TO info.
APPEND |Superclass of exception class: { superclass }| TO info.
"Using the 'previous' attribute to assign the previous exception
"object
acc_err = acc_err->previous.
"If there was no cast to cx_root, a cast would be required here,
"for example, as follows.
"acc_err = CAST #( acc_err->previous ).
ENDWHILE.
out->write( data = info name = `info` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `4) CLEANUP` ) ).
"The following example shows nested TRY control structures.
"The inner TRY control structure includes a CLEANUP block.
"The example demonstrates that an exception raised cannot
"be handled by the TRY control structure where it is raised.
"No meaningful 'cleanup' action is performed in the example's
"CLEANUP block. For demonstration purposes, an info table is
"populated demonstrating the execution of the block.
DATA info_tab TYPE string_table.
DATA cleanup TYPE REF TO cx_root.
DO 2 TIMES.
APPEND |---- Loop pass { sy-index } ----| TO info_tab.
TRY.
TRY.
IF sy-index = 1.
DATA(calc) = CONV decfloat34( 1 / 0 ).
ELSE.
DATA(strtab) = VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ).
DATA(line_4) = strtab[ 4 ].
ENDIF.
CATCH cx_sy_zerodivide INTO cleanup.
APPEND `---- Catching cx_sy_zerodivide ----` TO info_tab.
APPEND cleanup->get_text( ) TO info_tab.
CLEANUP.
APPEND `#### Executing CLEANUP block ####` TO info_tab.
APPEND `d` TO strtab.
"CLEANUP block must be executed completely
"Leaving the block prematurely causes a runtime error. If it is statically known
"that it is not possible to return to the block, a syntax error is shown.
"So, the following statement is not allowed in the CLEANUP block.
"RETURN.
ENDTRY.
CATCH cx_sy_itab_line_not_found INTO cleanup.
APPEND `---- Catching cx_sy_itab_line_not_found ----` TO info_tab.
APPEND cleanup->get_text( ) TO info_tab.
ENDTRY.
ENDDO.
out->write( data = info_tab name = `info_tab` ).
out->write( |\n| ).
out->write( |\n| ).
"CLEANUP ... INTO
"The example is the same as above. Here, the CLEANUP statement is specified
"with the addition INTO. If required, you can evaluate the exception information
"as shown above.
DATA info_tab_b TYPE string_table.
DATA cleanup_b TYPE REF TO cx_root.
DO 2 TIMES.
APPEND |---- Loop pass { sy-index } ----| TO info_tab_b.
TRY.
TRY.
IF sy-index = 1.
DATA(calc_b) = CONV decfloat34( 1 / 0 ).
ELSE.
DATA(strtab_b) = VALUE string_table( ( `a` ) ( `b` ) ( `c` ) ).
DATA(line_4_b) = strtab_b[ 4 ].
ENDIF.
CATCH cx_sy_zerodivide INTO cleanup_b.
APPEND `---- Catching cx_sy_zerodivide ----` TO info_tab_b.
APPEND cleanup_b->get_text( ) TO info_tab_b.
CLEANUP INTO cleanup_b.
APPEND `#### Executing CLEANUP block ####` TO info_tab_b.
"Using RTTI to find out the absolute name of the class of the raised execption
DATA(class_name) = cl_abap_classdescr=>get_class_name( p_object = cleanup_b ).
APPEND class_name TO info_tab_b.
APPEND `d` TO strtab_b.
"CLEANUP block must be executed completely
"Leaving the block prematurely causes a runtime error. If it is statically known
"that it is not possible to return to the block, a syntax error is shown.
"So, the following statement is not allowed in the CLEANUP block.
"RETURN.
ENDTRY.
CATCH cx_sy_itab_line_not_found INTO cleanup_b.
APPEND `---- Catching cx_sy_itab_line_not_found ----` TO info_tab_b.
APPEND cleanup_b->get_text( ) TO info_tab_b.
ENDTRY.
ENDDO.
out->write( data = info_tab_b name = `info_tab_b` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `5) RETRY` ) ).
"The following example includes a division of 1 by another number.
"In a DO loop 1 is divided by all numbers from 10 to -10.
"When the zero division exception is caught, the exception cause is
"removed by changing the first operand's value to 0, too, before the
"RETRY statement. The RETRY statement triggers the execution of the TRY
"control structure again, now resulting in 0 as division result. After
"that, and to have a self-contained example, the operand value is changed
"back to 1.
DATA division_result_tab TYPE TABLE OF decfloat34 WITH EMPTY KEY.
DATA(number1) = 1.
DATA(number2) = 11.
DATA division_result TYPE decfloat34.
DO.
number2 -= 1.
TRY.
division_result = number1 / number2.
APPEND division_result TO division_result_tab.
CATCH cx_sy_zerodivide.
"Removing the exception cause by setting a value that
"does not raise the zero division exception
number1 = 0.
DATA(retry_flag) = abap_true.
"Processing the TRY control structure again
RETRY.
ENDTRY.
"Resetting of number1 value to 1
IF retry_flag = abap_true.
number1 = 1.
retry_flag = abap_false.
ENDIF.
IF sy-index = 21.
EXIT.
ENDIF.
ENDDO.
out->write( data = division_result_tab name = `division_result_tab` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `6) RESUME` ) ).
DATA restab TYPE string_table.
TYPES ty_inttab TYPE TABLE OF i WITH EMPTY KEY.
DATA(inttab) = REDUCE ty_inttab( INIT tab = VALUE ty_inttab( )
FOR i = -5 UNTIL i > 5
NEXT tab = VALUE #( BASE tab ( i ) ) ).
LOOP AT inttab INTO DATA(wa).
TRY.
DATA(divres) = meth_resumable(
num1 = sy-tabix
num2 = wa
).
APPEND |{ sy-tabix } / { wa } = { divres }| TO restab.
CATCH BEFORE UNWIND zcx_demo_abap_error_b INTO DATA(error_resume).
DATA(is_resumable) = error_resume->is_resumable.
IF is_resumable IS NOT INITIAL.
APPEND |Exception raised. Is resumable? -> "{ is_resumable }"| TO restab.
RESUME.
ENDIF.
ENDTRY.
ENDLOOP.
out->write( data = restab name = `restab` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `7) MESSAGE` ) ).
"The following examples demonstrate several MESSAGE statements.
TYPES c50 TYPE c LENGTH 50.
DATA message_attribute_tab TYPE TABLE OF c50 WITH EMPTY KEY.
MESSAGE e001(zdemo_abap_messages) INTO DATA(msg).
DATA(msgid) = sy-msgid.
DATA(msgty) = sy-msgty.
message_attribute_tab = VALUE #( BASE message_attribute_tab ( msgid ) ( msgty ) ( '--------' ) ).
MESSAGE e004(zdemo_abap_messages)
WITH 'A' 'B' 'C' 'D'
INTO msg.
msgid = sy-msgid.
msgty = sy-msgty.
DATA(msgv1) = sy-msgv1.
DATA(msgv2) = sy-msgv2.
DATA(msgv3) = sy-msgv3.
DATA(msgv4) = sy-msgv4.
message_attribute_tab = VALUE #( BASE message_attribute_tab ( msgid ) ( msgty ) ( msgv1 )
( msgv2 ) ( msgv3 ) ( msgv4 )
( '--------' ) ).
MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER '005'
WITH 'E' 'F' 'G' 'H'
INTO msg.
msgid = sy-msgid.
msgty = sy-msgty.
msgv1 = sy-msgv1.
msgv2 = sy-msgv2.
msgv3 = sy-msgv3.
msgv4 = sy-msgv4.
message_attribute_tab = VALUE #( BASE message_attribute_tab ( msgid ) ( msgty ) ( msgv1 )
( msgv2 ) ( msgv3 ) ( msgv4 ) ).
out->write( data = message_attribute_tab name = `message_attribute_tab` ).
out->write( |\n| ).
"Getting all messages of a message class
"Note: When a message class/number is not found, the type
"is put in first position followed by the specified message
"name. Message numbers have a three-digit number.
DATA messages TYPE string_table.
DO 999 TIMES.
MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER sy-index
INTO DATA(msg_of_msgcl).
FIND PCRE `^E:ZDEMO_ABAP_MESSAGES` IN msg_of_msgcl.
IF sy-subrc <> 0.
APPEND |Message number { sy-index }: "{ msg_of_msgcl }"| TO messages.
ENDIF.
ENDDO.
out->write( data = messages name = `messages` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `8) Syntax Variants of RAISE EXCEPTION/THROW` ) ).
"In the example, the RAISE EXCEPTION/THROW statements are implemented in a method.
"Depending on a value, specific statements are called.
TYPES: BEGIN OF exception_info,
idx TYPE i,
exc_text TYPE string,
BEGIN OF source_position,
prog TYPE syrepid,
incl TYPE syrepid,
source_line TYPE i,
END OF source_position,
END OF exception_info,
ty_tab_exception_info TYPE TABLE OF exception_info WITH EMPTY KEY.
DATA tab_exception_info TYPE ty_tab_exception_info.
"Exception class implementing interface if_t100_message
DO.
TRY.
APPEND VALUE #( idx = sy-index ) TO tab_exception_info REFERENCE INTO DATA(line).
meth_a( sy-index ).
CATCH zcx_demo_abap_error_a INTO DATA(error_a).
line->exc_text = error_a->get_text( ).
error_a->get_source_position(
IMPORTING
program_name = line->source_position-prog
include_name = line->source_position-incl
source_line = line->source_position-source_line ).
ENDTRY.
IF sy-index = 20.
EXIT.
ENDIF.
ENDDO.
out->write( data = tab_exception_info name = `tab_exception_info` ).
CLEAR tab_exception_info.
"Exception class implementing interface if_t100_dyn_message
DO.
TRY.
APPEND VALUE #( idx = sy-index ) TO tab_exception_info REFERENCE INTO line.
meth_b( sy-index ).
CATCH zcx_demo_abap_error_b INTO DATA(error_b).
line->exc_text = error_b->get_text( ).
error_b->get_source_position(
IMPORTING
program_name = line->source_position-prog
include_name = line->source_position-incl
source_line = line->source_position-source_line ).
ENDTRY.
IF sy-index = 15.
EXIT.
ENDIF.
ENDDO.
out->write( data = tab_exception_info name = `tab_exception_info` ).
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `9) Excursions` ) ).
"Exploring the inheritance tree of exception classes
DATA inheritance_tree TYPE string_table.
DATA(class_name_table) = VALUE string_table( ( `CX_SY_ZERODIVIDE` )
( `CX_SY_ITAB_LINE_NOT_FOUND` )
( `CX_SY_CONVERSION_OVERFLOW` )
( `CX_SY_RTTI_TYPE_NOT_RELEASED` )
( `CX_ROOT` )
( `CX_STATIC_CHECK` )
( `CL_ABAP_TABLEDESCR` ) "Excursion: Not an exception class; class name of an RTTI class
( `CX_THIS_CLASS_DOES_NOT_EXIST` )
).
LOOP AT class_name_table INTO DATA(classname).
DO.
TRY.
cl_abap_typedescr=>describe_by_name( EXPORTING p_name = classname
RECEIVING p_descr_ref = DATA(tdo_type)
EXCEPTIONS type_not_found = 4 ) .
IF sy-subrc <> 0.
APPEND `--- Class not found ---` TO inheritance_tree.
EXIT.
ELSE.
APPEND classname TO inheritance_tree.
DATA(tdo_cl) = CAST cl_abap_classdescr( tdo_type ).
ENDIF.
"This method uses classic exceptions
tdo_cl->get_super_class_type(
RECEIVING p_descr_ref = DATA(tdo_super_class)
EXCEPTIONS super_class_not_found = 4 ).
IF sy-subrc <> 0.
EXIT.
ELSE.
classname = tdo_super_class->get_relative_name( ).
ENDIF.
CATCH cx_sy_rtti_type_not_released.
APPEND `--- Class not released ---` TO inheritance_tree.
EXIT.
ENDTRY.
ENDDO.
out->write( `-------------------` ).
out->write( data = inheritance_tree name = `inheritance_tree` ).
CLEAR inheritance_tree.
ENDLOOP.
out->write( |\n| ).
out->write( |\n| ).
"Exploration down the inheritance tree (from superclass to subclasses)
"You can use the XCO library. The example only uses examples with few
"subclasses and classes not high up in an inheritance tree to reduce
"the program runtime.
class_name_table = VALUE string_table( ( `CX_SY_ZERODIVIDE` )
( `CX_SY_RTTI_NO_CHECK` )
( `CL_ABAP_TYPEDESCR ` ) "Excursion: Not an exception class; class name of an RTTI class
( `CX_THIS_CLASS_DOES_NOT_EXIST` )
).
LOOP AT class_name_table INTO classname.
DATA(xco_handler) = xco_cp_abap=>class( CONV sxco_ao_object_name( classname ) ).
IF xco_handler->exists( ).
"Getting the names of the subclasses
DATA(subclass_names) = xco_handler->subclasses->all->get_names( ).
IF subclass_names IS INITIAL.
out->write( data = |The class { classname } has no subclasses.| ).
ELSE.
out->write( data = subclass_names name = `subclass_names` ).
ENDIF.
out->write( `-------------------` ).
ELSE.
out->write( |The class { classname } does not exist.| ).
ENDIF.
ENDLOOP.
out->write( |\n| ).
out->write( |\n| ).
"Evaluating the return value of a non-class-based exception and raising a class-based exception
TRY.
cl_abap_typedescr=>describe_by_name( EXPORTING p_name = 'CL_THAT_DOES_NOT_EXIST'
RECEIVING p_descr_ref = DATA(tdo_ty)
EXCEPTIONS type_not_found = 4 ).
IF sy-subrc <> 0.
RAISE EXCEPTION TYPE zcx_demo_abap_error_b MESSAGE e005(zdemo_abap_messages) WITH 'Type not found'.
ELSE.
DATA(tdo_class) = CAST cl_abap_classdescr( tdo_ty ).
"Getting more type information; find more details in the Dynamic Programming cheat sheet
...
ENDIF.
CATCH zcx_demo_abap_error_b INTO DATA(err).
DATA(error_text) = err->get_text( ).
out->write( data = error_text name = `error_text` ).
ENDTRY.
**********************************************************************
out->write( zcl_demo_abap_aux=>heading( `10) Programmatically Raising Runtime Erros` ) ).
out->write( `Obviously, the statements raising a runtime error are commented out :)` ).
"RAISE SHORTDUMP TYPE cx_sy_zerodivide.
DATA(flag) = abap_true.
"DATA(cond_w_throw_shortdump) = COND #( WHEN flag IS INITIAL THEN `works`
" ELSE THROW SHORTDUMP zcx_demo_abap_error_b( ) ).
"DATA(switch_w_throw_shortdump) = SWITCH #( flag WHEN '' THEN `works`
" ELSE THROW SHORTDUMP zcx_demo_abap_error_b( ) ).
DATA(number) = 0.
ASSERT number IS INITIAL.
ASSERT number > -1.
number = 1.
ASSERT number = 1.
flag = abap_false.
"ASSERT flag = abap_true.
ENDMETHOD.
METHOD meth_resumable.
IF num2 = 0 AND num1 <> 0.
RAISE RESUMABLE EXCEPTION TYPE zcx_demo_abap_error_b.
div_result = `No result. Resumed!`.
ELSE.
div_result = num1 / num2.
ENDIF.
"Statement with COND operator using RESUMABLE
"div_result = COND #( WHEN num2 = 0 AND num1 <> 0 THEN num1 / num2
" ELSE THROW RESUMABLE zcx_demo_abap_error( ) ).
ENDMETHOD.
METHOD meth_a.
"Note: A selection of additions is covered.
DATA(flag) = 'X'.
CASE num.
WHEN 1.
"TYPE addition
"Raises an exception of a specified exception class
RAISE EXCEPTION TYPE zcx_demo_abap_error_a.
WHEN 2.
"TYPE addition including EXPORTING
"Assigns actual parameters to input parameters of the instance constructor
"particularly for classes implementing if_t100_message. The example uses
"a constant structure having the same name as the exception class.
RAISE EXCEPTION TYPE zcx_demo_abap_error_a
EXPORTING
textid = zcx_demo_abap_error_a=>zcx_demo_abap_error_a.
WHEN 3.
"Assigning values to replace placeholders of a message
"In this case, replacing the placeholders &1 and &2 in a message.
RAISE EXCEPTION TYPE zcx_demo_abap_error_a
EXPORTING
textid = zcx_demo_abap_error_a=>error_003
p_003_a = `a`
p_003_b = `b`.
WHEN 4.
"Assigning values to replace placeholders of a message
"In this case, not all available parameters are filled.
RAISE EXCEPTION TYPE zcx_demo_abap_error_a
EXPORTING
textid = zcx_demo_abap_error_a=>error_004
p_004_a = `c`
p_004_b = `d`
p_004_c = `e`.
WHEN 5.
"Specifying an exception object
DATA(cx_oref) = NEW zcx_demo_abap_error_a( textid = zcx_demo_abap_error_a=>error_005
p_005_a = `Some`
p_005_b = `error` ).
RAISE EXCEPTION cx_oref.
WHEN 6.
"Eexception object created inline
RAISE EXCEPTION NEW zcx_demo_abap_error_a( textid = zcx_demo_abap_error_a=>error_004
p_004_a = `f`
p_004_b = `g` ).
WHEN 7.
"MESSAGE addition
"For passing message specification to an exception object
"Note that EXPORTING can also be specified
"After MESSAGE, a message type is specified (which is not of relevance in ABAP for Cloud
"Development). These types can be the following: A, E, I, S, W, or X. See the ABAP Keyword
"Documentation for information.For example, E representing an error message.
"The character is followed by the message number. The message class name is directly
"specified within a pair of parentheses.
RAISE EXCEPTION TYPE zcx_demo_abap_error_a MESSAGE e002(zdemo_abap_messages).
WHEN 8.
"Specifying a message number that does not exist
"The specified message type, message class, and message number are
"used as short text in uppercase letters and separated by a colon
RAISE EXCEPTION TYPE zcx_demo_abap_error_a MESSAGE e999(zdemo_abap_messages).
WHEN 9.
"MESSAGE, ID, TYPE, NUMBER additions
"The message class, the message type, and the message number are specified.
"Here, literals are specified. The example is an alternative to the one above.
"addition WITH not supported when interface not implemented
RAISE EXCEPTION TYPE zcx_demo_abap_error_a
MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER '002'.
WHEN 10.
"Specifying the values using data objects, not as literals as above.
"Note that uppercase letters are expected.
DATA(msgid) = 'ZDEMO_ABAP_MESSAGES'.
DATA(msgty) = 'E'.
DATA(msgnum) = '002'.
RAISE EXCEPTION TYPE zcx_demo_abap_error_a MESSAGE ID msgid TYPE msgty NUMBER msgnum.
WHEN 11.
"Intentionally using lowercase letters for demonstration purposes
msgid = to_lower( 'ZDEMO_ABAP_MESSAGES' ).
msgty = 'E'.
msgnum = '002'.
RAISE EXCEPTION TYPE zcx_demo_abap_error_a MESSAGE ID msgid TYPE msgty NUMBER msgnum.
WHEN 12.
"Using COND operator with various additions
DATA(cond_w_throw_1) = COND #( WHEN flag IS INITIAL THEN `works`
ELSE THROW zcx_demo_abap_error_a( ) ).
WHEN 13.
DATA(cond_w_throw_2) = COND #( WHEN flag IS INITIAL THEN `works`
ELSE THROW zcx_demo_abap_error_a( textid = zcx_demo_abap_error_a=>error_004
p_004_a = `cond_a`
p_004_b = `cond_b` ) ).
WHEN 14.
DATA(cond_w_throw_3) = COND #( WHEN flag IS INITIAL THEN `works`
ELSE THROW zcx_demo_abap_error_a( MESSAGE e005(zdemo_abap_messages)
p_005_a = `An exception raised with COND` ) ).
WHEN 15.
DATA(cond_w_throw_4) = COND #( WHEN flag IS INITIAL THEN `works`
ELSE THROW zcx_demo_abap_error_a( MESSAGE ID 'ZDEMO_ABAP_MESSAGES' TYPE 'E' NUMBER '002' ) ).
WHEN 16.
DATA(cond_w_throw_5) = COND #( WHEN flag IS INITIAL THEN `works`
ELSE THROW zcx_demo_abap_error_a( MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER '005'
p_005_a = `An exception raised with COND`
p_005_b = `additions MESSAGE ID/TYPE/NUMBER` ) ).
WHEN 17.
"Using SWITCH operator with various additions
DATA(switch_w_throw_1) = SWITCH #( flag WHEN '' THEN `works`
ELSE THROW zcx_demo_abap_error_a( ) ).
WHEN 18.
DATA(switch_w_throw_2) = SWITCH #( flag WHEN '' THEN `works`
ELSE THROW zcx_demo_abap_error_a( textid = zcx_demo_abap_error_a=>error_004
p_004_a = `switch_a`
p_004_b = `switch_b` ) ).
WHEN 19.
DATA(switch_w_throw_4) = SWITCH #( flag WHEN '' THEN `works`
ELSE THROW zcx_demo_abap_error_a( MESSAGE e005(zdemo_abap_messages)
p_005_a = `An exception raised with SWITCH` ) ).
WHEN 20.
DATA(switch_w_throw_3) = SWITCH #( flag WHEN '' THEN `works`
ELSE THROW zcx_demo_abap_error_a( MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER '005'
p_005_a = `An exception raised with SWITCH`
p_005_b = `additions MESSAGE ID/TYPE/NUMBER` ) ).
WHEN OTHERS.
RETURN.
ENDCASE.
ENDMETHOD.
METHOD meth_b.
"Note: A selection of additions is covered.
DATA(flag) = 'X'.
CASE num.
WHEN 1.
RAISE EXCEPTION TYPE zcx_demo_abap_error_b.
WHEN 2.
RAISE EXCEPTION TYPE zcx_demo_abap_error_b MESSAGE e002(zdemo_abap_messages).
WHEN 3.
RAISE EXCEPTION TYPE zcx_demo_abap_error_b
MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER '002'.
WHEN 4.
RAISE EXCEPTION TYPE zcx_demo_abap_error_b MESSAGE e005(zdemo_abap_messages) WITH 'Hello' 'world'.
WHEN 5.
RAISE EXCEPTION TYPE zcx_demo_abap_error_b MESSAGE ID 'ZDEMO_ABAP_MESSAGES'
TYPE 'E'
NUMBER '004'
WITH 'abc' 'def' 'ghi' 'jkl'.
WHEN 6.