Catching a custom exception with cfscript
Asked Answered
A

1

5

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");
}
Amidst answered 12/12, 2013 at 15:9 Comment(1)
I don't know but the first thing I would try is "throw".Predispose
I
9

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.

Il answered 12/12, 2013 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.