I need an exception object to test some code that ...
# cfml-general
a
I need an exception object to test some code that handles exceptions (not in the try/catch sense of handling, we just need to do some processing on an exception). I can't - for the life of me - think of a way in CFML to create an exception other than throwing one (like with
throw
). And to get the object I need to then catch it. In other languages I'd do some variation of
myException = new MyException()
Am I being dense, or is there no such functionality in CFML? I realise I can go
myJavaException = createObject("java", "MyJavaException").init()
, but the testing I need to do is specific to CFML exceptions.
t
if we are talking about lucee then I think they are all java exceptions in the package
lucee.runtime.exp
, createObject would work
βœ… 1
a
I'll see if any of that helps, cheers (and yes, it is with Lucee in this case)
r
I've handled this as you described by throwing and catching my test exception. Feels dirty but its simple.
βœ… 1
βž• 1
d
Dirty maybe, but lifelike, like what the code is actually doing, not a simulation or artificial construct.
t
Plus I find writing code that throws exceptions quite natural
s
there's another way to write code?
a
I always thought my code was... exceptional.
πŸ€¦πŸ»β€β™€οΈ 1
🀣 3
πŸ€¦β€β™‚οΈ 1
πŸ€¦β€β™€οΈ 2
s
I don't aim that high; but mine is very worthy of being thrown
πŸ˜‚ 2
😜 1
d
Why mess with artificial exceptions when you can throw 100% natural ones on demand? Here's a function that leverages the power of buggy code to throw any exception type except security (*not tested in Lucee):
Copy code
function throwException(type) {
	switch(type) {
		case "application": return fileRead("");
    	case "database": return queryExecute("",{},{datasource=""});
    	case "template": return evaluate("##");
    	case "object": return createObject("java",".");
		case "expression": return 0/0;
    	case "missingInclude": include "/";
    	case "lock": lock scope="" timeout=0 {};
	}
}
πŸ‘πŸ» 1
s
that's straight out of the necronomicon, that is
πŸ‘Ή 1
a
Not much of an answer to a question that was "how to get an exception object without just throwing an exception and catching it". Given your approach is precisely "just throwing an exception and catching it"
d
It's not much of an answer, but it's undeniably exceptional. And, to be fair, you did say "...other than throwing one (like withΒ 
throw
)". While my function does throw exceptions (what else would you do with exceptions?), they're not like the ones you get from
throw
.
😜 1
s
....in which the difference between throwing exceptions and throwing exceptionally is parsed
j
I'm just here for the comments...
a
// here you go then
t
<!--- don't want to feel left out --->