{"id":1346,"date":"2015-03-13T10:41:15","date_gmt":"2015-03-13T09:41:15","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=1346"},"modified":"2015-05-31T16:02:35","modified_gmt":"2015-05-31T15:02:35","slug":"abap-local-exception-class-using-if_t100_message","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=1346","title":{"rendered":"ABAP &#8211; Local Exception class using IF_T100_MESSAGE"},"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=\" size-full wp-image-358 alignleft\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>In this article I&#8217;ll show how to implement local exception class which will use all the benefits of interface IF_T100_MESSAGE &#8211; it will simply use the message classes for generating exception messages.<!--more--><\/p>\n<p>Here comes local exception class definition which:<\/p>\n<ul>\n<li>Implements interface IF_T100_MESSAGE<\/li>\n<li>Aliases IF_T100_MESSAGE~T100KEY to local T100KEY<\/li>\n<li>Defines 5 <em>PUBLIC<\/em> variables (MSGV1-4, WERKS) to be used in constructor to generate the message. Generally there can be any number and any <em>type<\/em> of variable &#8211; it allows type checking!!!)<\/li>\n<li>Defines your messages as class public constants (GENERAL_ERROR_4VARS, WRONG_PLANT), where the message class, message number and substitution variables (ATTR1-4) are assigned with name of the class PUBLIC variables (MSGV1-4, WERKS) &#8211; their contents will be used to substitute the &#8216;&amp;&#8217; in the message<\/li>\n<\/ul>\n<pre lang=\"abap\">CLASS lcx_exception DEFINITION\r\n  INHERITING FROM cx_static_check.\r\n  PUBLIC SECTION.\r\n    INTERFACES if_t100_message .\r\n\r\n    ALIASES t100key\r\n      FOR if_t100_message~t100key .\r\n\r\n    DATA:\r\n      msgv1 TYPE msgv1,\r\n      msgv2 TYPE msgv2,\r\n      msgv3 TYPE msgv3,\r\n      msgv4 TYPE msgv4,\r\n      werks TYPE werks_d.\r\n\r\n    METHODS: constructor\r\n      IMPORTING\r\n        textid   LIKE if_t100_message=&gt;t100key OPTIONAL\r\n        previous LIKE previous OPTIONAL\r\n        t100key  TYPE scx_t100key OPTIONAL \r\n        msgv1    TYPE msgv1 OPTIONAL\r\n        msgv2    TYPE msgv2 OPTIONAL\r\n        msgv3    TYPE msgv3 OPTIONAL\r\n        msgv4    TYPE msgv4 OPTIONAL\r\n        werks    TYPE werks_d OPTIONAL.\r\n\r\n    CONSTANTS:\r\n      BEGIN OF GENERAL_ERROR_4VARS,\r\n        msgid TYPE symsgid VALUE 'ZX01',        \" Message class(SE91)\r\n        msgno TYPE symsgno VALUE '000',\r\n        attr1 TYPE scx_attrname VALUE 'MSGV1',\r\n        attr2 TYPE scx_attrname VALUE 'MSGV2',\r\n        attr3 TYPE scx_attrname VALUE 'MSGV3',\r\n        attr4 TYPE scx_attrname VALUE 'MSGV4',\r\n      END OF GENERAL_ERROR_4VARS,\r\n\r\n      BEGIN OF WRONG_PLANT,\r\n        msgid TYPE symsgid VALUE 'ZX01',\r\n        msgno TYPE symsgno VALUE '001',\r\n        attr1 TYPE scx_attrname VALUE 'WERKS',\r\n        attr2 TYPE scx_attrname VALUE '',\r\n        attr3 TYPE scx_attrname VALUE '',\r\n        attr4 TYPE scx_attrname VALUE '',\r\n      END OF WRONG_PLANT.\r\nENDCLASS.               \"lcx_exception<\/pre>\n<p>The implementation of the exception class will now be simple as everything else is already defined in the standard super class <em>cx_static_check<\/em>.<\/p>\n<pre lang=\"abap\">CLASS lcx_exception IMPLEMENTATION.\r\n  METHOD constructor.\r\n    CALL METHOD super-&gt;constructor\r\n      EXPORTING\r\n        previous = previous.\r\n    me-&gt;msgv1 = msgv1 .\r\n    me-&gt;msgv2 = msgv2 .\r\n    me-&gt;msgv3 = msgv3 .\r\n    me-&gt;msgv4 = msgv4 .\r\n    me-&gt;werks = werks .\r\n\r\n    me-&gt;t100key = t100key .\r\n    CLEAR me-&gt;textid.\r\n    IF textid IS INITIAL.\r\n      if_t100_message~t100key = if_t100_message=&gt;default_textid.\r\n    ELSE.\r\n      if_t100_message~t100key = textid.\r\n    ENDIF.\r\n  ENDMETHOD.\r\nENDCLASS.               \"lcx_exception<\/pre>\n<p>Now let&#8217;s define our class which:<\/p>\n<ul>\n<li>Will use the local exception class in the method&#8217;s <strong>RAISING<\/strong> part of definition<\/li>\n<\/ul>\n<pre lang=\"abap\">CLASS lcl_demo_class DEFINITION\r\n  PUBLIC SECTION.\r\n    DATA:\r\n      mv_werks TYPE werks_d.\r\n\r\n    METHODS:\r\n      constructor IMPORTING iv_werks TYPE werks_d\r\n                  RAISING lcx_exception,\r\n      get_data\r\n        RAISING lcx_exception.\r\nENDCLASS.<\/pre>\n<p>Implementation of the local class will be raising our exceptions&#8230;<\/p>\n<pre lang=\"abap\">CLASS lcl_demo_class IMPLEMENTATION.\r\n  METHOD constructor.\r\n    IF iv_werks &lt;&gt; '1234'.\r\n      RAISE EXCEPTION TYPE lcx_exception\r\n        EXPORTING\r\n          textid = lcx_exception=&gt;WRONG_PLANT\r\n          werks  = iv_werks.\r\n    ENDIF.\r\n  ENDMETHOD.\r\n \r\n  METHOD get_data.\r\n*   If there is an error in your code -&gt; raise exception\r\n    IF 1 &lt;&gt; 2.\r\n      RAISE EXCEPTION TYPE lcx_exception\r\n        EXPORTING\r\n          textid = lcx_exception=&gt;GENERAL_ERROR_4VARS\r\n          msgv1  = |Something happened in our very beautiful|\r\n          msgv2  = |MEGA HYPER SUPER TROUPER plant:|\r\n          msgv3  = |{ mv_werks }|.\r\n    ENDIF.\r\n  ENDMETHOD.\r\nENDCLASS.<\/pre>\n<p>Now let&#8217;s use the prepared classes in real code:<\/p>\n<pre lang=\"abap\">DATA:\r\n  lo_demo_class TYPE REF TO lcl_demo_class,\r\n  lo_exception  TYPE REF TO lcx_exception.\r\n\r\n*************************************\r\n* Demo with wrong plant\r\nTRY.\r\n    CREATE OBJECT lo_demo_class\r\n      EXPORTING \r\n        iv_werks = '2345'.\r\n  CATCH lcx_exception INTO lo_exception.\r\n    MESSAGE lo_exception-&gt;get_text( ) TYPE 'S' DISPLAY LIKE 'E'.\r\nENDTRY.\r\n*************************************\r\n* Demo with correct plant which fails \r\n* however during GET_DATA method\r\n\r\n* Note: Each TRY-CATCH-ENDTRY should always be separated to\r\n* catch\/isolate single possible exception -&gt; \r\n* we have two blocks of TRY-CATCH-ENDTRY\r\nTRY.\r\n    CREATE OBJECT lo_demo_class\r\n      EXPORTING \r\n        iv_werks = '1234'.\r\n  CATCH lcx_exception INTO lo_exception.\r\n    MESSAGE lo_exception-&gt;get_text( ) TYPE 'S' DISPLAY LIKE 'E'.\r\nENDTRY.\r\nTRY.\r\n    lo_demo_class-&gt;get_data( ).\r\n  CATCH lcx_exception INTO lo_exception.\r\n    MESSAGE lo_exception-&gt;get_text( ) TYPE 'S' DISPLAY LIKE 'E'.\r\nENDTRY.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this article I&#8217;ll show how to implement local exception class which will use all the benefits of interface IF_T100_MESSAGE &#8211; it will simply use the message classes for generating exception messages.<\/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 - Local Exception class using IF_T100_MESSAGE","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,15,9],"tags":[446,194,414,221],"class_list":["post-1346","post","type-post","status-publish","format-standard","hentry","category-abap","category-algorithms","category-development","tag-abap","tag-exceptions","tag-if_t100_message","tag-oo-abap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-lI","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1346","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=1346"}],"version-history":[{"count":4,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1346\/revisions"}],"predecessor-version":[{"id":1429,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/1346\/revisions\/1429"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}