{"id":36,"date":"2008-07-02T10:51:41","date_gmt":"2008-07-02T16:51:41","guid":{"rendered":"http:\/\/www.n8williams.com\/devblog\/?p=36"},"modified":"2008-12-07T17:49:20","modified_gmt":"2008-12-07T23:49:20","slug":"throw-and-try-catch-in-cfscript","status":"publish","type":"post","link":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript","title":{"rendered":"Throw errors in a cfscript and try catch in cfscript"},"content":{"rendered":"<p>You CAN&#8217;T throw an error or errors from a cfscript without defining your own throw function. But this is pretty easy to do. Somewhere else in your code, just write a &lt;cfunction name=&#8221;throw&#8221; etc. and in this function just put a &lt;cfthrow tag that throws the error you want to be thrown. A good practice would be to include a throw function in a utility cfc that you could invoke wherever you needed to throw an error in a cfscript.<\/p>\n<p>See the example code below. Note the interesting syntax for catching in a cfscript where you do catch(ERRORTYPE CFCATCHVARIABLE). So catch(database e), catch(any e) would be some common ones. And then you could access e.Message, e.Detail, etc.<\/p>\n<div class=\"text-box\"><code>try<br \/>\n{<br \/>\n     \/\/nothing below the throw would execute in this example.<br \/>\n     \/\/But I left the rest for syntax reference.<br \/>\n     somethingWrong=true;<br \/>\n     if(somethingWrong){<br \/>\n          throw(&quot;ExampleErrorType&quot;,&quot;Example Error message.&quot;);<br \/>\n}<br \/>\n     \/\/save the reactor record without interfering with custom transaction<br \/>\n     template1.save(useTransaction=false);<br \/>\n}<br \/>\ncatch (database err)<br \/>\n{<br \/>\n     \/\/if the save fails and the error is a database error, return the error detail.<br \/>\n     \/\/otherwise this error will need to be caught by the code calling this code.<br \/>\n     return  err.Detail;<br \/>\n}<br \/>\n<\/code><\/div>\n<p>If you want to do a rethrow, the following article has a great example.<br \/>\n<a title=\"http:\/\/www.coldfusionjedi.com\/index.cfm?mode=entry&amp;entry=3089633C-9FA0-606B-3F540AE9642A795F\" href=\"http:\/\/www.coldfusionjedi.com\/index.cfm?mode=entry&amp;entry=3089633C-9FA0-606B-3F540AE9642A795F\" target=\"_blank\">http:\/\/www.coldfusionjedi.com\/index.cfm?mode=entry&amp;entry=3089633C-9FA0-606B-3F540AE9642A795F<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You CAN&#8217;T throw an error or errors from a cfscript without defining your own throw function. But this is pretty easy to do. Somewhere else in your code, just write a &lt;cfunction name=&#8221;throw&#8221; etc. and in this function just put a &lt;cfthrow tag that throws the error you want to be thrown. A good practice &hellip; <a href=\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Throw errors in a cfscript and try catch in cfscript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.11 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Throw errors in a cfscript and try catch in cfscript - The Dev Pages<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Throw errors in a cfscript and try catch in cfscript - The Dev Pages\" \/>\n<meta property=\"og:description\" content=\"You CAN&#8217;T throw an error or errors from a cfscript without defining your own throw function. But this is pretty easy to do. Somewhere else in your code, just write a &lt;cfunction name=&#8221;throw&#8221; etc. and in this function just put a &lt;cfthrow tag that throws the error you want to be thrown. A good practice &hellip; Continue reading &quot;Throw errors in a cfscript and try catch in cfscript&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\" \/>\n<meta property=\"og:site_name\" content=\"The Dev Pages\" \/>\n<meta property=\"article:published_time\" content=\"2008-07-02T16:51:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2008-12-07T23:49:20+00:00\" \/>\n<meta name=\"author\" content=\"Nate Admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@admin\" \/>\n<meta name=\"twitter:site\" content=\"@admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nate Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#article\",\"isPartOf\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\"},\"author\":{\"name\":\"Nate Admin\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"headline\":\"Throw errors in a cfscript and try catch in cfscript\",\"datePublished\":\"2008-07-02T16:51:41+00:00\",\"dateModified\":\"2008-12-07T23:49:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\"},\"wordCount\":167,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"articleSection\":[\"Coldfusion\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\",\"url\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\",\"name\":\"Throw errors in a cfscript and try catch in cfscript - The Dev Pages\",\"isPartOf\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#website\"},\"datePublished\":\"2008-07-02T16:51:41+00:00\",\"dateModified\":\"2008-12-07T23:49:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/n8williams.com\/devblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Throw errors in a cfscript and try catch in cfscript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#website\",\"url\":\"https:\/\/n8williams.com\/devblog\/\",\"name\":\"The Dev Pages\",\"description\":\"A knowledge base for web applications development (and beyond)\",\"publisher\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/n8williams.com\/devblog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\",\"name\":\"Nate Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g\",\"caption\":\"Nate Admin\"},\"logo\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/n8williams.com\",\"https:\/\/twitter.com\/admin\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Throw errors in a cfscript and try catch in cfscript - The Dev Pages","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript","og_locale":"en_US","og_type":"article","og_title":"Throw errors in a cfscript and try catch in cfscript - The Dev Pages","og_description":"You CAN&#8217;T throw an error or errors from a cfscript without defining your own throw function. But this is pretty easy to do. Somewhere else in your code, just write a &lt;cfunction name=&#8221;throw&#8221; etc. and in this function just put a &lt;cfthrow tag that throws the error you want to be thrown. A good practice &hellip; Continue reading \"Throw errors in a cfscript and try catch in cfscript\"","og_url":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript","og_site_name":"The Dev Pages","article_published_time":"2008-07-02T16:51:41+00:00","article_modified_time":"2008-12-07T23:49:20+00:00","author":"Nate Admin","twitter_card":"summary_large_image","twitter_creator":"@admin","twitter_site":"@admin","twitter_misc":{"Written by":"Nate Admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#article","isPartOf":{"@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript"},"author":{"name":"Nate Admin","@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"headline":"Throw errors in a cfscript and try catch in cfscript","datePublished":"2008-07-02T16:51:41+00:00","dateModified":"2008-12-07T23:49:20+00:00","mainEntityOfPage":{"@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript"},"wordCount":167,"commentCount":0,"publisher":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"articleSection":["Coldfusion"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#respond"]}]},{"@type":"WebPage","@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript","url":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript","name":"Throw errors in a cfscript and try catch in cfscript - The Dev Pages","isPartOf":{"@id":"https:\/\/n8williams.com\/devblog\/#website"},"datePublished":"2008-07-02T16:51:41+00:00","dateModified":"2008-12-07T23:49:20+00:00","breadcrumb":{"@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/n8williams.com\/devblog\/coldfusion\/throw-and-try-catch-in-cfscript#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/n8williams.com\/devblog\/"},{"@type":"ListItem","position":2,"name":"Throw errors in a cfscript and try catch in cfscript"}]},{"@type":"WebSite","@id":"https:\/\/n8williams.com\/devblog\/#website","url":"https:\/\/n8williams.com\/devblog\/","name":"The Dev Pages","description":"A knowledge base for web applications development (and beyond)","publisher":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/n8williams.com\/devblog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757","name":"Nate Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g","caption":"Nate Admin"},"logo":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/n8williams.com","https:\/\/twitter.com\/admin"]}]}},"_links":{"self":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":0,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"wp:attachment":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}