• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling in Java  RSS feed

 
Guru MaheswaraReddy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you please let me know When do we need to go for User defined Exception?

I saw in some code people are catching run time exceptions also.
For example



In the above program it throws IllegalArgumentException when the userName is null.

Do we need to catch this type of exception and show to the user in the UI?
If I am not showing to user, Then user can't know what is happening.

Could you please clarify on this?

Thanks,
Guru
[ December 04, 2008: Message edited by: Martijn Verburg ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you want to intimate the user that whatever data he/she has entered
is not valid. But telling the user that its IllegalArgumentException would
seems bit jargon. Instead if you say something like "Invalid value for field
<fieldName>", it would be quite comprehensive to the user or even you
maintain the log of the exceptions.

Sample user defined exception for your use case:


Your code would look leveraging more object oriented programming paradigm
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are losing a lot of functionality in the toString method if you simply return the message. Probably better to return super.toString() + " " + message;
Or better still, to see the javadoc for IllegalArgumentException and see what its toString method returns, before trying to override the method.

And your constructor looks peculiar; you are setting the message field twice. In fact that probably won't compile if "message" is private.

To create your own Exception you need to create 4 (overloaded) constructors to match the superclass constructor. If there isn't a constructor with "cause" in, invoke the setCause method from the constructor. In 99% of cases, that will be all you need to do.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To catch a null value, it is probably better to throw a NullPointerException. Better to have it propagate to the JVM and crash the application or the thread than let the user get away with passing null.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell,
Agreed!


you are setting the message field twice. In fact that probably won't compile
if "message" is private.


I didn't get this well!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to check the message field; if it has private access in the Throwable or Exception classes, then you cannot set it in the constructor; you can only say super(message).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!