Ernesto Arroyo wrote:Although using the constructor is valid, this other solution is better because we can return null
Ernesto Arroyo wrote:Although using the constructor is valid, this other solution is better because we can return null:
Ernesto Arroyo wrote:
Anyway both options could be appropiate, depending of circumstances.
Bear Bibeault wrote:I can think of numerous occasions where I have used the Factory pattern rather than rely upon an exception.
I would use an exception if the condition is indeed exceptional, but use a Factory if failure was something that could be expected and then an alternate approach taken.
Bear Bibeault wrote:Again, if it's an exceptional condition that needs the stack to unwind, I'd throw an exception.
If I just wanted to continue processing in the same method, I'd use a Factory that returns null on failure. An if statement is a lot clearer than a try-catch block that simply catches the exception and continues on its merry way.
Jeff Verdegan wrote:I'd certainly consider the caller passing me a param that violates a precondition to be an exceptional condition.
True, but an if statement is less clear
I expect that we just have two very different mental pictures of what the underlying assumptions are that define the context we're talking about.)
Ernesto Arroyo wrote:I could prefer to return a NullPerson (Null Object Pattern)
Ernesto Arroyo wrote:Oh!
Not only I prefer return null but I could prefer to return a NullPerson (Null Object Pattern)
It depends but a NullPerson who does nothing can be (or cannot, it depends) a great design.
Lio Liov wrote:Most of the posts are not from me ,who started the thread
Lio Liov wrote:
I am just trying to learn and nobody answer why my code doesnt print the IllegalArgumentException,the code from the second post
Lio Liov wrote:Thanks
I am using Eclipse and i doesnt show anithing
Ernesto Arroyo wrote:
You throw the exception, but you have to catch it in your main ( or before )
and print a message (system.out or log4j better, but log4j maybe is a little more advanced now if you are learning)
So use a try-catch in the main and in the catch print the message you want.
Don't get me started about those stupid light bulbs. |