General J2EE: Is right a findermethod throws an exception?
Hello dudes, there's something bother me a lot these days, why i need to throw a FinderException if it cannot find any register in the database that represents the primaryKey? Why not just to return a NULL primaryKey object? Regards, lottalava
- Posted by: Edson Alves Pereira
- Posted on: November 04 2006 09:42 EST
Well! first of all! Exceptions are not your enemies, they are like symptoms that tell you that you are going to fall sick.If you have a finder Exception then you know that the key does not exist in the database, so you think of something else to handle the situation, returning null won't do because database won't allow null as keys and your are returning null as a key to indicate that its not there and may be in future you try to do something with it and get NullPointerException. Its not about returning null, its about telling that it din't find your key.Now its up to you what you want to do return null or your name. cheers, http://www.javaicillusion.blogspot.com/
More to that..J2EE follows some standards and specifications. Its the standard that the FinderException exception must be included in the throws clause of every findMETHOD(...) method of an entity Bean's home interface. The exception is used as a standard application-level exception to report a failure to find the requested EJB object(s).Based on these specific exception, container takes appropriate actions and helps you to guide properly, else on returning NULL, at what point down the line, your application would crash, nobody knows!!