• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Exception - checked Or Unchecked  RSS feed

 
Vaibhav Gargs
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever we are creating custom exceptions, then which exception we should create: Checked or Unchecked? What is the best practice to follow?
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you using the Exceptions for?
 
Vaibhav Gargs
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently, we are creating it to validate the data.
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you use illegal argument exception or similar?

Tell us what the procedure is when incorrect data are entered.
 
Vaibhav Gargs
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are building the REST API. We are logging the error message in the logs and returning a message to the caller about the same.
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, are you always handling the Exception? Does that give any hints about whether it shou‍ld be checked or unchecked?
 
Vaibhav Gargs
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell. Got it, it should be a Checked one

Any scenario where we should ever create a Custom Exception as Non-Checked? I mean if we don't need to handle any exception, then does it make sense to create it as Unchecked one?
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
I would have thought you would usually make your custom exception checked. Some people have one single Exception class which all custom exceptions are subtypes of.You can write catch (ProjectException exc) ... and catch everything.
 
Vaibhav Gargs
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell!

I am just wondering if we ever need to create Unchecked Exception ever ourselves? Any realtime scenario will help to understand it better.
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i never remember creating unchecked exception myself
 
Campbell Ritchie
Marshal
Posts: 55785
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could consider OverflowException extends ArithmeticException or DivideByZeroException which also extends ArithmeticException. Now there are methods in Java8 for overflow‑resistant arithmetic, that may be less useful now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!