[ajug-members] Checked vs. Unchecked Exceptions

Jason Chambers tooger at bellsouth.net
Wed Jun 28 21:28:46 EDT 2006


Inspired by Spring and by being influenced by a colleague to think  
differently about exceptions (hello Yao!), in recent times I have  
switched to the unchecked exception model (in other words my  
exceptions inherit from RuntimeException) for my APIs. This gives the  
client more flexibility in deciding to let exceptions ripple up or  
catch the exception and do something meaningful about it.  It can  
actually reduce coupling if you think about it (if you choose to let  
it ripple up, each method in the call stack has to make the same  
decision adding un-intended dependencies to your API).

However, even though I use RuntimeException derivatives I still make  
sure they are in the throws clause.

On Jun 27, 2006, at 12:52 PM, Bill Siggelkow wrote:

>
> If by "hunt around" you mean "read the javadoc" then you are right.  
> You can use @throws for any type of exception.  But you (and the  
> Gosling interview) make some good points. In many respects the  
> answer is "it depends". What Bruce Eckel points out is that the  
> unintended effect of checked exceptions has led to some pretty  
> heinous anti-patterns.
>
> On Jun 27, 2006, at 11:49 AM, Dan Marchant wrote:
>
>> The problem with that is the Remote exception could be a  
>> recoverable situation.
>> For instance you attempt to connect to a primary host and it fails
>> within your app you have a fallback secondary host to attempt.
>>
>> If the RemoteException is a RuntimeException someone when developing
>> the failover code would have to hunt around through documentation to
>> decide on the proper Exception to catch. This creates problems when
>> you try and develop fault  tolerant systems.
>>
>> For your normal basic application without any requirements for being
>> stable sure go ahead through the runtime exceptions and let the JVM
>> deal with the problem.
>>
>> Good questions to bring up with Gosling when he comes up... or just
>> read this: http://www.artima.com/intv/solid.html
>>
>> With RuntimeExceptions it is just as easy for some developer to see
>> the pattern someone set and put in:
>>
>> if(str == null) {
>>   throw new RuntimeException("My String was Null" + str);
>> }
>>
>>
>> Then later on put:
>> try {
>>   getString(str);
>> } catch(RuntimeException re) {
>>   log.error("Couldn't get the string",re);
>> }
>>
>> Now what is the point of the runtime exception and what other side
>> effects will this introduce?
>>
>> My 2 cents.
>>
>> - Dan
>>
>>
>>
>>
>> On 6/27/06, Bill Siggelkow <bsiggelkow at mac.com> wrote:
>>> I agree with Rob,  thereby transitively agree with Bruce. I can't
>>> tell you how many times I have had to yell at developers who write
>>> code like
>>>
>>> try {
>>>    ...
>>> }
>>> catch (Exception e) {
>>>    e.printStackTrace()
>>> }
>>>
>>> This is the worst code in the world; the printing of the stack trace
>>> only mildly helps -- it assumes that a pair of eyes is watching the
>>> console -- which in most cases is not true. I use checked exceptions
>>> in those clear cases where the caller of the API can reasonably be
>>> expected to take some action based on the exception. Consider an
>>> authentication system:
>>>
>>> public User authenticate(String username, String password) throws
>>> AuthenticationException;
>>>
>>> Here, we could have subclasses of AuthenticationException such as
>>> UserNotFoundException, InvalidPasswordException,
>>> ExpiredPasswordException, etc.
>>>
>>> There is a general trend toward RuntimeException usage -- look at
>>> what Spring does with it's proxying support for remote services --
>>> one  big thing  that it does is convert checked exceptions to
>>> subclasses of RuntimeException.
>>>
>>> What I tend to do these days is throw a subclass of  
>>> RuntimeException,
>>> and then document what the exception is in case someone wants to
>>> catch it. This is the kind of behavior that Bruce describes with
>>> Python. Ruby takes a similar approach.
>>>
>>>
>>> On Jun 27, 2006, at 9:05 AM, Rob Worsnop wrote:
>>>
>>> > On 6/26/06, Justin Fiore <justin.fiore at gmail.com> wrote:
>>> >> Hello All,
>>> >>
>>> >> I am interested in your opinions of the Checked vs. Unchecked
>>> >> Exception
>>> >> debate.
>>> >>
>>> >> Any answers can be from a theorhetical or conceptual standpoint
>>> >> (excluding
>>> >> any language specific implementations), or concrete, and or  
>>> practical
>>> >> scenarios.
>>> >>
>>> >> If this has already been discussed, please direct me to the
>>> >> discussion.
>>> >>
>>> >
>>> > I agree with Bruce Eckel:
>>> > http://www.mindview.net/Etc/Discussions/CheckedExceptions
>>> >
>>> > If you don't, there are plenty more opinions to choose from:
>>> > http://www.google.com/search?q=checked+vs+unchecked+exceptions
>>> >
>>> > Good luck with your assignment.
>>> > _______________________________________________
>>> > ajug-members mailing list
>>> > ajug-members at ajug.org
>>> > http://www.ajug.org/mailman/listinfo/ajug-members
>>>
>>> _______________________________________________
>>> ajug-members mailing list
>>> ajug-members at ajug.org
>>> http://www.ajug.org/mailman/listinfo/ajug-members
>>>
>> _______________________________________________
>> ajug-members mailing list
>> ajug-members at ajug.org
>> http://www.ajug.org/mailman/listinfo/ajug-members
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members




More information about the ajug-members mailing list