http://coldfusion.com logo
#cfml-general
Title
# cfml-general
a

Adam Cameron

02/16/2022, 11:03 AM
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

thisOldDave

02/16/2022, 11:23 AM
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

Adam Cameron

02/16/2022, 12:30 PM
I'll see if any of that helps, cheers (and yes, it is with Lucee in this case)
r

rory

02/16/2022, 12:31 PM
I've handled this as you described by throwing and catching my test exception. Feels dirty but its simple.
βœ… 1
βž• 1
d

Dave Merrill

02/16/2022, 1:58 PM
Dirty maybe, but lifelike, like what the code is actually doing, not a simulation or artificial construct.
t

thisOldDave

02/16/2022, 1:59 PM
Plus I find writing code that throws exceptions quite natural
s

sknowlton

02/16/2022, 2:18 PM
there's another way to write code?
a

Adam Cameron

02/16/2022, 2:30 PM
I always thought my code was... exceptional.
πŸ€¦πŸ»β€β™€οΈ 1
🀣 3
πŸ€¦β€β™‚οΈ 1
πŸ€¦β€β™€οΈ 2
s

sknowlton

02/16/2022, 2:34 PM
I don't aim that high; but mine is very worthy of being thrown
πŸ˜‚ 2
😜 1
d

David Buck

02/17/2022, 2:35 AM
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

sknowlton

02/17/2022, 3:04 AM
that's straight out of the necronomicon, that is
πŸ‘Ή 1
a

Adam Cameron

02/17/2022, 8:05 AM
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

David Buck

02/17/2022, 2:27 PM
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

sknowlton

02/17/2022, 2:30 PM
....in which the difference between throwing exceptions and throwing exceptionally is parsed
j

Jonas Eriksson

02/18/2022, 10:37 AM
I'm just here for the comments...
a

Adam Cameron

02/18/2022, 10:49 AM
// here you go then
t

thisOldDave

02/18/2022, 10:50 AM
<!--- don't want to feel left out --->