{"id":721,"date":"2013-10-08T13:38:57","date_gmt":"2013-10-08T12:38:57","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=721"},"modified":"2013-10-08T13:51:12","modified_gmt":"2013-10-08T12:51:12","slug":"abap-class-based-exceptions","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=721","title":{"rendered":"ABAP &#8211; Class based exceptions"},"content":{"rendered":"<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\"><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\" \/><\/a>There are several ways how to handle errors or warnings in ABAP and in this article I&#8217;ll try to present exceptions. Specifically &#8211; global class based exceptions in ABAP<!--more--><\/p>\n<ol>\n<li>The most elegant way how to handle exception messages is to have them all stored at one place &#8211; ideally in a message class. So the first step will be message class creation (if you don&#8217;t already have one) in SE91.<br \/>\nIn this message class you can create your desired messages &#8211; you can use the most generic message<\/p>\n<pre lang=\"abap\" gutter=\"false\">&amp; &amp; &amp; &amp;<\/pre>\n<p>but\u00a0in our example we&#8217;ll use the following:<\/p>\n<pre lang=\"abap\" gutter=\"false\">Material &amp; does not exist<\/pre>\n<p style=\"text-align: left;\"><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"722\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=722\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01.png\" data-orig-size=\"735,273\" 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=\"Messages definition\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01.png\" class=\"size-medium wp-image-722 alignnone\" alt=\"Messages definition\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01-300x111.png\" width=\"300\" height=\"111\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01-300x111.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01-500x185.png 500w, https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_01.png 735w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<\/li>\n<li>Now we can step forward to create the exception class itself:<br \/>\nIn SE24 create new class of type &#8220;Exception&#8221; named i.e. ZCX_MY_EXCEPTION derived\/inherited (in most cases)\u00a0from CX_STATIC_CHECK and check the option &#8220;With Message Class&#8221;<br \/>\n<a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_02.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"723\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=723\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_02.png\" data-orig-size=\"349,383\" 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=\"Exception class creation\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_02.png\" class=\"size-medium wp-image-723 alignnone\" alt=\"Exception class creation\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_02-273x300.png\" width=\"273\" height=\"300\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_02-273x300.png 273w, https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_02.png 349w\" sizes=\"auto, (max-width: 273px) 100vw, 273px\" \/><\/a><\/li>\n<li>Note: Step 1 and checking option &#8220;With message class&#8221; in step 2 can be ommited and\u00a0texts will be created and stored in the text management OTR. But it is not possible to combine both text variants in one exception class.<\/li>\n<li>Because we have parameters in our exception messages we have to define these parameters in our exception class. The most generic would be standard MSGV1 &#8211; MSGV4<br \/>\n<a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_03.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"724\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=724\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_03.png\" data-orig-size=\"\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"Exception class parameters\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_03.png\" class=\"wp-image-724 alignnone\" alt=\"Exception class parameters\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_03.png\" width=\"722\" height=\"402\" \/><\/a><\/li>\n<li>Now we extend our exception by adding new specific exception IDs. To do that, go to Texts tab and create new exception ID, i.e. MATERIAL_NOT_FOUND and assign it with message class, message number and bind any message attributes (&amp; in the message text) to the corresponding exception attributes.<br \/>\n<a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_04.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"726\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=726\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_04.png\" data-orig-size=\"\" data-comments-opened=\"1\" data-image-meta=\"[]\" data-image-title=\"Exception class Exception IDs\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_04.png\" class=\" wp-image-726 alignnone\" alt=\"Exception class Exception IDs\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_04.png\" width=\"925\" height=\"505\" \/><\/a><\/li>\n<li><em>Note:<\/em> If you don&#8217;t specify Exception ID when raising the exception then the default exception ID (it has the same name as exception class) is used and it&#8217;s text is generated.<\/li>\n<li>And that&#8217;s it!<br \/>\nNow you can simply use your new exception class with specific exception IDs in your program like in the following code:<\/p>\n<pre lang=\"abap\" gutter=\"false\">DATA\u00a0l_exc\u00a0TYPE REF TO zcx_my_exception.\r\n\r\nTRY.\r\n* Exception thrown\r\n  RAISE EXCEPTION TYPE zcx_my_exception\r\n    EXPORTING\r\n      textid = zcx_my_exception=&gt;material_not_found\r\n      msgv1  = '1234567'.                     \"material number\r\nCATCH zcx_my_exception into l_exc.\r\n  message l_exc type 'E'.\r\nendtry.<\/pre>\n<p>Your MESSAGE is displayed automatically, with the attribute values inserted. There are no calls to get_text() or get_longtext(), and no data declarations for the resulting message string.<br \/>\n<a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_05.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"728\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=728\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_05.png\" data-orig-size=\"212,31\" 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=\"Exception thrown\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_05.png\" class=\"size-full wp-image-728 alignnone\" alt=\"Exception thrown\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/CLASS_BASED_EXCEPTIONS_05.png\" width=\"212\" height=\"31\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>There are several ways how to handle errors or warnings in ABAP and in this article I&#8217;ll try to present exceptions. Specifically &#8211; global class based exceptions in ABAP<\/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":"","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":[446,146,194,195],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","tag-abap","tag-class","tag-exceptions","tag-oo"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-bD","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/721","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=721"}],"version-history":[{"count":5,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/721\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}