{"id":859,"date":"2013-11-19T11:05:42","date_gmt":"2013-11-19T10:05:42","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=859"},"modified":"2013-11-19T11:05:42","modified_gmt":"2013-11-19T10:05:42","slug":"abap-resumable-exceptions","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=859","title":{"rendered":"ABAP &#8211; Resumable exceptions"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"alignleft size-full wp-image-358\" alt=\"SAP\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" width=\"44\" height=\"50\" \/>Resume code execution after an exception has been thrown can be very useful in several different scenarios (i.e. processing multiple materials and you want to process all but skip only those where there&#8217;s an error) This can be solved quite easily using <em>resumable exceptions<!--more--><\/em><\/p>\n<p>First we define our exception class (can be global &#8211; defined in DDIC or locally defined). Nothing special needs to be specified.<\/p>\n<pre lang=\"abap\">CLASS lcx_my_exception DEFINITION\r\n  INHERITING FROM cx_static_check.\r\n  PUBLIC SECTION.\r\n    DATA:\r\n      message TYPE char50.\r\n\r\n    METHODS:\r\n      constructor\r\n        IMPORTING\r\n          message TYPE char50 OPTIONAL.\r\nENDCLASS.                    \"LCX_MY_EXCEPTION DEFINITION\r\n\r\nCLASS lcx_my_exception IMPLEMENTATION.\r\n  METHOD constructor.\r\n    super-&gt;constructor( ).\r\n    me-&gt;message = message.\r\n  ENDMETHOD.                    \"CONSTRUCTOR\r\nENDCLASS.                    \"LCX_MY_EXCEPTION IMPLEMENTATION<\/pre>\n<p>We will demonstrate both the classic exception and resumable exception on a division operation with methods <em>DIVIDE<\/em> and <em>DIVIDE_RESUMABLE<\/em><\/p>\n<pre lang=\"abap\">CLASS lcl_math IMPLEMENTATION.\r\n  METHOD divide_resumable.\r\n    TRY.\r\n      IF i_denominator = 0.\r\n        RAISE RESUMABLE EXCEPTION TYPE lcx_my_exception\r\n          EXPORTING\r\n            message = 'Divide by zero with the following values:'.\r\n      ELSE.\r\n        result = i_numerator \/ i_denominator.\r\n      ENDIF.\r\n\r\n      WRITE:\/ 'Numerator:', i_numerator,\r\n              'Denominator:', i_denominator.\r\n    ENDTRY.\r\n  ENDMETHOD.                    \"divide_resumable\r\n\r\n  METHOD divide.\r\n    IF i_denominator = 0.\r\n      RAISE EXCEPTION TYPE lcx_my_exception\r\n        EXPORTING\r\n          message = 'Divide by zero with the following values:'.\r\n    ELSE.\r\n      result = i_numerator \/ i_denominator.\r\n    ENDIF.\r\n\r\n* !!! NOT RESUMABLE !!!\r\n* The following line will never be hit when denominator = 0\r\n    WRITE:\/ 'Numerator:', i_numerator,\r\n            'Denominator:', i_denominator.\r\n  ENDMETHOD.                    \"divide\r\nENDCLASS.                    \"lcl_math IMPLEMENTATION<\/pre>\n<p><em>Note the different raising commands in:<br \/>\n<\/em>Method <em>DIVIDE_RESUMABLE<\/em><\/p>\n<pre lang=\"abap\">RAISE RESUMABLE EXCEPTION TYPE lcx_my_exception<\/pre>\n<p>Method <em>DIVIDE<\/em><\/p>\n<pre lang=\"abap\">RAISE EXCEPTION TYPE lcx_my_exception<\/pre>\n<p>Here follows a demo program that calls the division operation in both resumable and non-resumable form:<\/p>\n<pre lang=\"abap\">DATA:\r\n  gr_ex    TYPE REF TO lcx_my_exception,\r\n  g_result TYPE anzh2,\r\n  g_err    TYPE abap_bool.\r\n\r\nSTART-OF-SELECTION.\r\n  TRY.\r\n      g_result = lcl_math=&gt;divide(\r\n        i_numerator = 10\r\n        i_denominator = 0\r\n      ).\r\n\r\n*     !!! NOT RESUMABLE !!!\r\n*     The following CASE block will never be executed when denominator = 0\r\n      CASE g_err.\r\n        WHEN space.\r\n          WRITE:\/ 'Result:', g_result.\r\n        WHEN OTHERS.\r\n          WRITE:\/ 'Result undefined'.\r\n      ENDCASE.\r\n\r\n    CATCH lcx_my_exception INTO gr_ex.\r\n      g_err = abap_true.\r\n      WRITE:\/ gr_ex-&gt;message.\r\n  ENDTRY.\r\n\r\n  SKIP 1.\r\n  CLEAR:\r\n    g_err,\r\n    gr_ex.\r\n\r\n  TRY.\r\n      g_result = lcl_math=&gt;divide_resumable(\r\n        i_numerator = 10\r\n        i_denominator = 0\r\n      ).\r\n\r\n      CASE g_err.\r\n        WHEN space.\r\n          WRITE:\/ 'Result:', g_result.\r\n        WHEN OTHERS.\r\n          WRITE:\/ 'Result undefined'.\r\n      ENDCASE.\r\n\r\n    CATCH BEFORE UNWIND lcx_my_exception INTO gr_ex.\r\n      WRITE:\/ gr_ex-&gt;message.\r\n\r\n      IF gr_ex-&gt;is_resumable = abap_true.\r\n        g_err = abap_true.\r\n        RESUME.\r\n      ENDIF.\r\n  ENDTRY.<\/pre>\n<p><em>Note the different catch commands for:<br \/>\n<\/em>Method <em>DIVIDE_RESUMABLE<\/em> (calls <em>RESUME<\/em> which returns to previous processing right after command which raised this exception)<\/p>\n<pre lang=\"abap\">CATCH BEFORE UNWIND lcx_my_exception INTO gr_ex.\r\n...\r\nRESUME<\/pre>\n<p>Method <em>DIVIDE<\/em><\/p>\n<pre lang=\"abap\">CATCH lcx_my_exception INTO gr_ex.<\/pre>\n<p>And the output of this simple from is shown on follwing screenshot<a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/RESUMABLE_EXCEPTIONS.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"861\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=861\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/RESUMABLE_EXCEPTIONS.png\" data-orig-size=\"358,108\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Output of demo program for Resumable exceptions\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/RESUMABLE_EXCEPTIONS.png\" class=\"size-medium wp-image-861 alignnone\" alt=\"Output of demo program for Resumable exceptions\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/RESUMABLE_EXCEPTIONS-300x90.png\" width=\"300\" height=\"90\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/RESUMABLE_EXCEPTIONS-300x90.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/RESUMABLE_EXCEPTIONS.png 358w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Resume code execution after an exception has been thrown can be very useful in several different scenarios (i.e. processing multiple materials and you want to process all but skip only those where there&#8217;s an error) This can be solved quite &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=859\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - Resumable exceptions http:\/\/wp.me\/p3nYbe-dR","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,9],"tags":[],"class_list":["post-859","post","type-post","status-publish","format-standard","hentry","category-abap","category-development"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-dR","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=859"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/859\/revisions"}],"predecessor-version":[{"id":862,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/859\/revisions\/862"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}