Exception or not?

Even in the official java API, Exceptions did not put an end to the old habit of "return null if the element is not present" or "return -1 if ..".

But exception were not invented for this?

on the other hand, Exception always require the "try" and "catch" blocks that make readability harder. using the -1 or null techniques makes calling code more compact.

when designing, when Execeptions should be used? when one can avoid it?

what you guys say?

[478 byte] By [xxx-uffa] at [2007-9-30 20:16:12]
# 1

The biggest thing to keep in mind is that Exceptions should be used to handle "exceptions to normal processing". If it's normal to return null when an element is not found in a HashMap for example, it is not considered an exception. On the other hand, if it is not normal for a null value to be passed into a method, throwing an exception give the developer a gracefull way of dealing with such an occurance.

By the way, "try/catch" are only mandatory for checked exceptions. And personally, I don't think "try/catch" blocks make readability harder.

Just a thought. Hope it helps.

mgumbs at 2007-7-7 1:01:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

You use checked exceptions to indicate exceptional conditions that a user might reasonably recover from. If a SQL connection times out and throws a SQLException, it's reasonable to expect that a user might recover by reopening the connection.

If a user tries to dereference a null reference, a NPE will be thrown - an unchecked exception. It's less likely that a reasonable recovery strategy is available in that case.

The other rule of thumb is: don't use Exceptions and try/catch blocks for program flow control. Throwing an exception should be a truly exceptional thing, NOT a sophisticated go-to.

%

duffymo at 2007-7-7 1:01:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

On the other hand, if it is not normal for a null value to be passed into a method, throwing an exception give the developer a gracefull way of dealing with such an occurance.

does it mean that in principle in any method I develop I should check if the input is null and if so throw the appropiate exception?

never did that..

is it not thrown automatically a RunTime Excpetions?

xxx-uffa at 2007-7-7 1:01:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> On the other hand, if it is not normal for a null

> value to be passed into a method, throwing an

> exception give the developer a gracefull way of

> dealing with such an occurance.

>

> does it mean that in principle in any method I develop

> I should check if the input is null and if so throw

> the appropiate exception?

Not necessarily. I was just providing an example of when a exception should be thrown. duffymo put it best with the following statement.

The other rule of thumb is: don't use Exceptions and try/catch blocks for program flow control. Throwing an exception should be a truly exceptional thing, NOT a sophisticated go-to.

mgumbs at 2007-7-7 1:01:25 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
You may also want to look at [url= http://c2.com/cgi/wiki?NullObject]introducing a Null Object[/url]...
yawmark at 2007-7-7 1:01:25 > top of Java-index,Other Topics,Patterns & OO Design...