View Full Version : Java - Force Exception or use an if-then-else structure
Roelf
10-17-2006, 08:58 AM
Hi,
I am having a discussion with a colleague about the following. When developing some logic to see if something exists and if it doesn't, then create it, he thinks the following solution is the preferred one:
OurObject obj;
// create a reference to the object if it exists, if it doesn't exist, the method just returns null
obj = someotherobject.somemethodwhichreturnsthe-existingobject(argument)
try {
// next call will throw exception if object was not found in before call
obj.somemethod();
} catch (Exception e) {
// apparently the object did not exist, so let's create it
obj = someobject.createobject(arguments);
}
i think its more correct to do it this way:
OurObject obj;
// create a reference to the object if it exists, if it doesn't exist, the method just returns null
obj = someotherobject.somemethodwhichreturnsthe-existingobject(argument)
if (object == null) {
// apparently the object did not exist, so let's create it
obj = someobject.createobject(arguments);
}
what do you think is better/faster/whatever
Brandoe85
10-17-2006, 11:36 AM
I'm with the if statement logic. Try catches are slow(or is it they use to be?). And speed is really irrelevant to me here, the if statement seems the most logical. Now, maybe it's a different story if when you do the assignment it throws an exception if it doesn't exist.
Hope that makes some sense of my opinion :)
Aradon
10-17-2006, 06:51 PM
Hi,
I am having a discussion with a colleague about the following. When developing some logic to see if something exists and if it doesn't, then create it, he thinks the following solution is the preferred one:
OurObject obj;
// create a reference to the object if it exists, if it doesn't exist, the method just returns null
obj = someotherobject.somemethodwhichreturnsthe-existingobject(argument)
try {
// next call will throw exception if object was not found in before call
obj.somemethod();
} catch (Exception e) {
// apparently the object did not exist, so let's create it
obj = someobject.createobject(arguments);
}
i think its more correct to do it this way:
OurObject obj;
// create a reference to the object if it exists, if it doesn't exist, the method just returns null
obj = someotherobject.somemethodwhichreturnsthe-existingobject(argument)
if (object == null) {
// apparently the object did not exist, so let's create it
obj = someobject.createobject(arguments);
}
what do you think is better/faster/whatever
I think it depends on the context of the overall program and what you're doing. For example whether you think it's better for a method to throw an exception for breaking reasons or if you need it to continue.
Personally just looking at that code I'd say the second one is faster assembly wise as well as more logical looking. But the first one has various uses which could extend to several different applications.
Time wise? You'll probably not notice any real significance unless you're building a very poorly written program, to which it'll spread out.
You could always test it by using timers to see how long execution of a particular statement is. :P
Beagle
10-17-2006, 08:18 PM
I think that exception handling is just that, the exception. You use exception handling when expectations are not met. If your logic allows for the object to not be created, then I call it "Expection handling" because your expecting it to happen. (Yeah, it's really expectation, but it's funnier this way).
So, let's say your fully expecting someone to pass an object as a variable, and instead they pass null, you can decide to throw an exception there if nulls are not allowed.
In your example, if the object you get back could logically be null, that means the function your calling is specific to have a null value as one of the possible returns. This is NOT an exception, it is an expectation.
Besides, your catching all exceptions with that code. What if the object that gets returned is fine, but the function your calling is throwing some other exception for some other reason? Your squelching that error and not ever going to be able to see it.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.