This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this the way I should use custom exception?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RegistrationCustomException class code
UserRegistrationDemo class: Uses custom exception classes

Can anyone help me to understand when I should use custom exception? Here I created custom exception class just to check age is between 18 and 100. Was that really necessary? I mean rather writing separate custom exception class, shouldn't it have been better if I had just checked age using if condition and print message and prompt user to enter valid age? I just want to know why should I need to create class just to check age range or name contains valid characters or not or does have valid length or not ?
In custom exception classes I tried to name them more expressive so I can understand what this exception is really about and where might have occured. I read about it here and coderanch old post and here coderanch old post Did I use the way they expressed how to and when to use custom exceptions?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Carey Brown
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
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.
 
Carey Brown
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your nested Exception class is not standard and makes for a less readable throw statement.
vs

Custom Exceptions often begin with some prefix that is consistent for an entire package, e.g. SQL....Exception, or MyPkg.....Exception.

Custom Exception constructors should be able to take an optional message as an argument.

Your use of NumberFormatException is flawed. For all other exceptions your print a stack trace. You should do the same for NumberFormatException.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getters are not consistent with the convention that a getter is expected to return something; probably String in your case.

Your getDetailMessage() method is called when the CustomeException is constructed. This will cause console output even if a section of code wants to absorb the specific exception. What if you want to use a Logger instead at some future time?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your nested Exception class is not standard and makes for a less readable throw statement.
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
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:Would you please give me an example so I could understand when and what sort of custom exception I should create?
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.

On a smaller scale, let's say you have a large number of classes that provide a front end interface to a database. You may want the API methods to return a consistent custom exception for that whole collection of code. In this case, you may want to catch SQL exceptions and then throw one of your own in addition to throwing one of your custom exceptions when it's an error in your API methods that may not be an SQL problem.

Example:

One thing missing from the above example is additional constructors that take a "Throwable cause" parameter. This is highly useful. If your DB API should throw an SQL exception you may want to replace it with a custom exception, but you can add the original SQL exception as a "cause".

The trick is to not be so specific with your low level package exceptions that they can't be reused. Often an exception can be reused just by changing the message that is passed in.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if I want to keep this class file in a package. Which class should be public in this? because I'm writing this in a package named mycstexpkg ?
 
Carey Brown
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Two separate files.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh ! I'm sorry but I need to understand this completely so I can move to last concept which is left i.e. suppressed exception in try with resources. I'll write complete code and post again. Thank you so much for help
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyPkgException file:

MyPkgInvalidInputException file:

CustomExceptionDemo file:
At line 13 in CustomExceptionDemo do we always have to pass such message to parameterized constructors? Can't we write that in the custom exception class it self? but in inbuilt Java's exception we don't pass such message String. Here also we could have written if condition like, if user input is invalid then print message so user can understand he/she given wrong input then why to write custom exception for this. I know I'm bit hassling you but please
 
Carey Brown
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception msg should tell why the exception is being thrown. Example
or, if you don't want to make a custom exception
or

Where "input" is the String that was found to be invalid.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I have found few cases where custom exceptions are necessary, usually there is an Exception that already exists that is close enough. The need would come out of making jars, packages, or subsystems for others to use.
 
Carey Brown
Bartender
Posts: 2990
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception "message" is usually used for debugging purposes. These messages need to include enough information to begin to understand what exceptional event caused the Exception to be thrown. These messages often appear on the console (not a good practice for production code), or log files (preferred).

Aside from the message, which message class you use depends on how you want the caller to catch the exception. If you catch it yourself it doesn't really matter. If the caller may write code that has the potential of encountering more than one type of exception then it would help them to differentiate your Exception class from the generic "Exception" class.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I'll keep this in mind while creating custom exception. Thank you so much Carey Brown
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!