Being Java programmer.
The Java™ Tutorials wrote:You should write your own exception classes if you answer yes to any of the following questions; otherwise, you can probably use someone else's.
1.Do you need an exception type that isn't represented by those in the Java platform?
2.Would it help users if they could differentiate your exceptions from those thrown by classes written by other vendors?
3.Does your code throw more than one related exception?
4.If you use someone else's exceptions, will users have access to those exceptions? A similar question is, should your package be independent and self-contained?
Being Java programmer.
1. No, you can use message for that. Additionally a message can echo back the actual input that was invalid.Ganish Patil wrote:I found this one good: The Java™ Tutorials
The Java™ Tutorials wrote:You should write your own exception classes if you answer yes to any of the following questions; otherwise, you can probably use someone else's.
1.Do you need an exception type that isn't represented by those in the Java platform?
2.Would it help users if they could differentiate your exceptions from those thrown by classes written by other vendors?
3.Does your code throw more than one related exception?
4.If you use someone else's exceptions, will users have access to those exceptions? A similar question is, should your package be independent and self-contained?
Answer
1. Yes, as I'm checking valid range of age and valid age format i.e. It should not contain special characters.
2. Yes, as I have used all custom exception except NumberFormatException.
3 and 4 I didn't get this, what do they mean?
yes I do agree even I seen so many examples on internet like mine above which could have been done using well descriptive messages in if conditions, It means I should not create custom exception for such purposes. Would you please give me an example so I could understand when and what sort of custom exception I should create?Carey Brown wrote: 1. No, you can use message for that. Additionally a message can echo back the actual input that was invalid.
2. Maybe you'd want one custom Exception but what you show is better dealt with by descriptive messages. Look at all your catch blocks that essentially do the same thing.
Being Java programmer.
yes I was also skeptical about that then I thought it would be good chance to use inner classes so used. Yes the second one looks easy to read. yes will keep in mind next time while naming packages.Carey Brown wrote:Your nested Exception class is not standard and makes for a less readable throw statement.
Being Java programmer.
If you are creating jar file(s) for others to use in their projects without expecting them to know the inner workings of your code, you may want a custom exception for the entire package so that the caller could tell instantly that an exception came from your package. And, in that case you would probably create more custom subclass exceptions for specific events. This also permits the caller the ability to catch a high level package exception or the more specific package exception.Ganish Patil wrote:Would you please give me an example so I could understand when and what sort of custom exception I should create?
Being Java programmer.
Being Java programmer.
Being Java programmer.
Being Java programmer.
Ganish Patil wrote:but again same question kept haunting me what is the main purpose of creating custom exceptions. Here we also seem to just print different messages with help of different classses.