Hi guys, I have a question about creating custom exception classes,
if your application is error free by using try and catches, what is the need to create exception classes?
I ask this because my application is working great now, but i was told to create a couple of custom exceptions,
and i am finding it hard to understand why i need them now, and where to use them.
If anybody maybe has some examples of where they used some before to help me get a better understanding of this
would be highly appreciated.
Hi custom exceptions can be of great help to pinpoint what the exception is. For example if you have a program that have workers buy sandwiches. Each worker has his own budget.
Imagine the following scenarios: 1) a worker doesn't have enough money to a sandwich and 2) a sandwich is already sold.
Now in your program you may not know which of these caused the program to fail. Is it the sandwich sold or worker not having enough money. Therefore you may want exceptions say SandwichAlreadySoldException and WorkerInsufficientMoneyException to indicate such situations.
This way in your code, instead of catching all exceptions with the Exceptions class, you can catch each individual exception and print a specific message saying the sandwich sold or worker not enough money.
You can also create a superclass called SandwichException or similar. You now have all your exceptions inthe same hierarchy, so you can catch any type with catch (SandwichException sexc) …
Joined: Nov 22, 2012
I see what your saying and thanks for the reply, but wouldn't it be easier to just use a if statement so that when the
workers tries to buy something and does not have enough money, you can return a message through the if statement
saying "Not enough money to purchase this sandwich" or whatever, if you get me.
For example if you were searching for a person through his ID number, would that be a situation where i could create a custom exception class that will
throw an exception if the user was to enter a character instead of a number?
Yes, if you can prevent an exception from happening before it does, you should. But there are cases where you can't do that - and at some point an exception is going to get thrown that is unpredicted - a null pointer exception or illegal argument exception, or sql exception... It is much more informative if you make a granular and meaningful exception so you know what happened because of the type of the exception that got thrown.
In your search for a person by ID example: there could be different causes for failure when searching for the ID. For example, the input might not be in the correct format, the person being searched for may not exist, or there could be a problem with the database (or XML file) where the data is stored. If you use InvalidIDFormatException, IDNotFoundException, and PersonDatasourceNotAvailableException make it much more obvious what caused the problem without exposing the underlying implementation details (what tool is used to validate the ID format, or if the person datasource is SQL or XML, or something else).
For me, there are two purposes for using custom exceptions:
1) Granular definition of exactly what wrong - in a manner relevant to what the user was doing (search for persons by ID) instead of relying on generic non-descriptive exceptions (IllegalArgument, NullPointer, NumberFormat...).
2) Expose possible problems without exposing implementation details. SQLExceptions, IOExceptions, and the like expose what technology you are using. If you change the technology you have to change the method signatures for any method throwing exceptions, and that breaks client code.
Joined: Oct 13, 2005
Depends on how the code is being used. If you are directly interacting with a user and can get them to change their input, then you probably don’t need an Exception. If you have code which is running without interaction, and allowing it to continue would breach the invariants of your class, then you are probably better off throwing an Exception.
Joined: Nov 22, 2012
Thanks for all your help guys, I understand why, where and when to use it now.