{"id":1485,"date":"2016-02-15T10:58:29","date_gmt":"2016-02-15T09:58:29","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1485"},"modified":"2023-11-13T14:10:59","modified_gmt":"2023-11-13T13:10:59","slug":"abap-catch-error-messages-from-function-modules","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1485","title":{"rendered":"ABAP &#8211; catch error messages from function modules"},"content":{"rendered":"<p>It it easy to catch named exceptions thrown by a\u00a0function module in section EXCEPTIONS when calling such FM. But what if an error message is thrown inside the FM without specifying the exception name?<\/p>\n<p><!--more--><\/p>\n<p>Most of the error messages thrown within a FM are exceptions done by <strong>RAISE<\/strong> or <strong>MESSAGE<\/strong> <strong>RAISING<\/strong> together with the exception name. Such messages can be caught by explicitly mentioning the exception name in the <strong>EXCEPTIONS<\/strong> section of the function module call or by using the &#8220;<strong>OTHERS<\/strong>&#8221; which collects all remaining\u00a0exception names.<\/p>\n<p>But what if there&#8217;s a message thrown within a function module where the exception name is not specified?<\/p>\n<p>You can use addition <strong>error_message = n_error<\/strong><\/p>\n<p>If the predefined exception<strong> error_message<\/strong> is specified after <strong>EXCEPTIONS<\/strong>, all messages<\/p>\n<ul>\n<li>that are sent using the statement <strong>MESSAGE<\/strong> <strong>without the addition RAISING<\/strong>,<\/li>\n<li>that are sent using the statement <strong>MESSAGE RAISING because no return code is assigned to them<\/strong>,<\/li>\n<li>that are sent by the <strong>ABAP runtime environment<\/strong><\/li>\n<\/ul>\n<p>during function module processing are affected as follows:<\/p>\n<ul>\n<li>Messages of the type <strong>S<\/strong>, <strong>I<\/strong>, or <strong>W<\/strong> are not sent but are flagged in the log in background processing.<\/li>\n<li>Messages of the type <strong>E<\/strong> and <strong>A<\/strong> raise the exception <strong>error_message<\/strong> and set sy-subrc to n_error. The message class, message type, message number, and the contents of possible placeholders for the MESSAGE statement are in the fields sy-msgid, sy-msgno, sy-msgty, and sy-msgv1, &#8230; sy-msgv4. <span style=\"color: #ff0000;\"><strong>With messages of the type A, the statement ROLLBACK WORK is also executed explicitly.<\/strong><\/span><\/li>\n<li>Messages of the type <strong>X<\/strong> are not affected &#8211; they cause a program termination with a short dump.<\/li>\n<\/ul>\n<p>You can test and verify it using this quick example code:<\/p>\n<p>Here comes the function module<\/p>\n<pre lang=\"abap\">FUNCTION z_mvoy_error.\n*\"----------------------------------------------------------------------\n*\"*\"Local Interface:\n*\"  IMPORTING\n*\"     REFERENCE(IV_IS_NORMAL_EXCEPTION) TYPE  FLAG\n*\"  EXCEPTIONS\n*\"      NORMAL_EXCEPTION\n*\"----------------------------------------------------------------------\n  IF iv_is_normal_exception = 'X'.\n\n    MESSAGE 'Cool, you can easily handle me...' TYPE 'E'\n      RAISING normal_exception.\n  ELSE.\n    MESSAGE 'Ooops, who can handle me???'       TYPE 'E'.\n  ENDIF.\n\nENDFUNCTION.<\/pre>\n<p>&#8230;and here&#8217;s the report that calls such function module<\/p>\n<pre lang=\"abap\">SELECTION-SCREEN BEGIN OF BLOCK bl01.\n  SELECTION-SCREEN BEGIN OF LINE.\n    SELECTION-SCREEN COMMENT 1(20) text_p01 FOR FIELD p_1.\n    PARAMETERS:  p_1 RADIOBUTTON GROUP g1 DEFAULT 'X'.\n  SELECTION-SCREEN END OF LINE.\n\n  SELECTION-SCREEN BEGIN OF LINE.\n    SELECTION-SCREEN COMMENT 1(20) text_p02 FOR FIELD p_2.\n    PARAMETERS:  p_2 RADIOBUTTON GROUP g1.\n  SELECTION-SCREEN END OF LINE.\n\n  SELECTION-SCREEN BEGIN OF LINE.\n    SELECTION-SCREEN COMMENT 1(20) text_p03 FOR FIELD p_3.\n    PARAMETERS:  p_3 RADIOBUTTON GROUP g1.\n  SELECTION-SCREEN END OF LINE.\n\nSELECTION-SCREEN END OF BLOCK bl01.\n\nINITIALIZATION.\n  text_p01 = 'MESSAGE-RAISING'.\n  text_p02 = 'MESSAGE - not handled'.\n  text_p03 = 'MESSAGE - handled'.\n\nSTART-OF-SELECTION.\n\n\n  CASE 'X'.\n    WHEN p_1.\n      CALL FUNCTION 'Z_MVOY_ERROR'\n        EXPORTING\n          iv_is_normal_exception = 'X'\n        EXCEPTIONS\n          normal_exception       = 1\n          OTHERS                 = 2.\n      IF sy-subrc &lt;&gt; 0.\n        WRITE: 'Normal exception created in FM with MESSAGE ... RAISING'.\n        WRITE:\/ 'SY-SUBRC: ', sy-subrc.\n        WRITE:\/ 'Message: ', sy-msgv1.\n      ENDIF.\n\n    WHEN p_2.\n      CALL FUNCTION 'Z_MVOY_ERROR'\n        EXPORTING\n          iv_is_normal_exception = ' '\n        EXCEPTIONS\n          normal_exception       = 1\n          OTHERS                 = 2.\n\n*     These lines will never be reached\n      IF sy-subrc &lt;&gt; 0.\n        WRITE: 'Error in FM with MESSAGE without clause RAISING'.\n        WRITE:\/ 'SY-SUBRC: ', sy-subrc.\n        WRITE:\/ 'Message: ', sy-msgv1.\n      ENDIF.\n\n    WHEN p_3.\n      CALL FUNCTION 'Z_MVOY_ERROR'\n        EXPORTING\n          iv_is_normal_exception = ' '\n        EXCEPTIONS\n          normal_exception       = 1\n          error_message          = 98\n          OTHERS                 = 99.\n\n      IF sy-subrc = 98.\n        WRITE: 'Error in FM with MESSAGE without clause RAISING ... caught in caller program'.\n        WRITE:\/ 'SY-SUBRC: ', sy-subrc.\n        WRITE:\/ 'Message: ', sy-msgv1.\n      ENDIF.\n\n  ENDCASE.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It it easy to catch named exceptions thrown by a\u00a0function module in section EXCEPTIONS when calling such FM. But what if an error message is thrown inside the FM without specifying the exception name?<\/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 - catch error messages from function modules","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":[463,153],"class_list":["post-1485","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","tag-error_message","tag-exception"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-nX","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1485","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=1485"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1485\/revisions"}],"predecessor-version":[{"id":1488,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1485\/revisions\/1488"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}