Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching a custom exception with cfscript

How does one catch a custom exception with try-catch in cfscript?

<cffunction name="myFunction">
  <cfset foo = 1>

  <cfif foo EQ 1>
    <cfthrow type="customExcp" message="FAIL!">
  </cfif>
</cfif>

The try-catch is in cfscript. What should go into the catch() statement?

try {
  myFunction();
} catch () {
  writeOutput("Ooops");
}
like image 410
RHPT Avatar asked Dec 12 '13 15:12

RHPT


1 Answers

James has pointed you to the docs in his answer, but he's missed the bit about you asking about custom exceptions. The syntax is:

try {
    myFunction();
} catch (customExcp e) {
    writeOutput("Ooops");
    writeDump(e); // have a look at the contents of this
}

Note you can have as many catch blocks as you like, for different exception types. Any exception type not explicitly caught will still be thrown.

like image 58
Adam Cameron Avatar answered Sep 19 '22 05:09

Adam Cameron