{"id":54224,"date":"2022-01-03T16:49:39","date_gmt":"2022-01-03T11:19:39","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=54224"},"modified":"2022-01-13T11:49:00","modified_gmt":"2022-01-13T06:19:00","slug":"handling-exceptions-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/handling-exceptions-in-grails\/","title":{"rendered":"Handling Exceptions in Grails"},"content":{"rendered":"<p>Daahh !! Who doesn&#8217;t know how to handle exceptions. A simple try-catch block would do the job. But think of so many methods where we will have to keep the same code of catching exceptions and then creating the desired response.<\/p>\n<p>Hmm, haven&#8217;t you heard of the common try-catch in the filter? Well, we can do that!<\/p>\n<p><em>But let&#8217;s say we have a list of exceptions to handle. We will have to take care of the hierarchy and keep adding multiple catch blocks as per that since our response code will be different for the different exceptions.<\/em><\/p>\n<p>From Grails 2.3.0 we can use declarative Controller Exception Handling.<br \/>\nAll you have to do is to create a <strong>handleYourExceptoion()<\/strong> method in your controller. Yes, you read it right, no annotation just the method with &#8216;handle &#8216; as the prefix, like below (code is in Groovy).<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54312 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-scaled.jpg\" alt=\"Handle\" width=\"2560\" height=\"453\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-300x53.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-1024x181.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-768x136.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-1536x272.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-2048x363.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-01-624x111.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/p>\n<p>And let&#8217;s say you have your own exception to handle just create a method like below (code is in Groovy).<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54313 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-scaled.jpg\" alt=\"Handle\" width=\"2560\" height=\"453\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-300x53.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-1024x181.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-768x136.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-1536x272.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-2048x363.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-02-624x111.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/p>\n<p>Now, will you be writing these handlers in every Controller? Of course not. Create an AbstractController, and extend this class in your Controller where you want to handle exceptions. Like below (code is in Groovy).<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54315 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-scaled.jpg\" alt=\"\" width=\"2560\" height=\"1644\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-300x193.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-1024x658.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-768x493.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-1536x986.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-2048x1315.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2022\/01\/Blog1-03-1-624x401.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Daahh !! Who doesn&#8217;t know how to handle exceptions. A simple try-catch block would do the job. But think of so many methods where we will have to keep the same code of catching exceptions and then creating the desired response. Hmm, haven&#8217;t you heard of the common try-catch in the filter? Well, we can [&hellip;]<\/p>\n","protected":false},"author":1393,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":90},"categories":[7,446,1994,1],"tags":[4916,4840,9,4844],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54224"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1393"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=54224"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54224\/revisions"}],"predecessor-version":[{"id":54316,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54224\/revisions\/54316"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=54224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=54224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=54224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}