ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Best Practices for Exception Handling
Subject:   Worst Practices
Date:   2003-11-20 11:53:11
From:   anonymous2

>"We could add useful methods to DuplicateUsernameException,..."


It would be useful to add some contructors.
The article failed to describe wrapped (or nested) exceptions: http://www.javaworld.com/javaworld/javatips/jw-javatip91-p3.html



>"Checked exceptions are also accused of breaking encapsulation. Consider the following:
List getAllAccounts()throwsFileNotFoundException"


Checked exceptions do not break encapsulation. Converting the low level FileNotFoundException into another checked exception demonstrates this.



>"If the client cannot do anything useful, then make the exception unchecked. "


The trouble is that you do NOT know if clients
can(not) do anything useful. It is better to give clients the option.


"In this example, the finally closes the connection and throws a RuntimeException if there is problem with closing the connection."


Dangerous. Throwing Exceptions from a the finally block may result in losing the original exception.


Throwing generic Excpetions(& RuntimeExceptions) is often a bad idea: http://www.javaworld.com/javaworld/jw-10-2003/jw-1003-generics.html?


"In an EJB throwing a system exception (RemoteException, RuntimeException or Error) does discard the EJB instance "


I agree.This article was directed at newbies.Here is someting better for newbies:
http://www.churchillobjects.com/c/11012.html


Here is a better article of Best Practices:
http://www-106.ibm.com/developerworks/java/library/j-ejbexcept.html



"It took the C++ community several years to decide on how to use exceptions. This debate has just started in the Java community."


I have not seen any debate- Just MUD (MS FUD):
http://www.artima.com/intv/handcuffs.html





1 to 1 of 1
  1. Worst Practices
    2003-11-20 12:19:26  anonymous2 [View]

1 to 1 of 1