Hi - How do I throw an exception? In that I want t...
# cfml-general
g
Hi - How do I throw an exception? In that I want to to write;
if( myVar = 2) {
throw("myVar = 2. It should never be 2");
}
But it fails - because it isn't in a try/catch. Only - I don't want to catch it. I want it to blow up the request.
b
I have no clue what you're asking, but
Copy code
if( myVar = 2)
should be
Copy code
if( myVar == 2)
g
Thanks @bdw429s My issue wasn't the IF - that was just poor typing, here, in SLACK. I was thinking my issue was that I HAD TO use throw() WITHIN try/catch and I didn't want to. I wanted to create an "unhandled" exception. And so trying to JUST use throw() in the IF - without the try/catch - was a the problem I was trying to solve - as my code wouldn't compile. But the issue was the syntax I was using in the throw() - NOT the fact that it was / wasn't in a try/catch block.
b
That's why it's important for you to share the exact code you are using and the exact error you receive when you run it rather than just saying "it fails" 🙂
Glad you sorted it
g
In my defence: The docs state;
Throws a developer-specified exception, which can be caught with a <cfcatch> tag
And because I was trying to use throw without the try/catch - I "assumed" - that was my issue. Especially since all the examples include a try/catch, too. It never occurred to me that I wasn't using the correct argument. Anyway - thanks again!