Any big reasons to define custome exceptions in Java?
Two reasons immediately spring to mind:
- Simply so you don't say
try { ... } catch (Exception e) { ... }
- having your own subclasses lets you treat distinct exception cases separately. (Such as the difference between not having permissions to run a report, and the report execution failing). - You can add extra context - so for example if you have your own
AlreadyLoggedInException
, say, that exception can have a method to retrieve the IP address from which the other session was started. Or anAccountLimitExceededException
could contain the current account limit. Extra information in the exception allows you to potentially take a more well-informed response when catching it.
Quite simply, it allows you to deal with each exception in the correct way.
Consider the following code
try {
doSomethingThatCouldThrowManyExceptions();
}
catch (ExceptionalCircumstance1 ex) {
// deal with this specific circumstance
}
catch (ExceptionalCircumstance2 ex) {
// deal with this specific circumstance
}
catch (ExceptionalCircumstance3 ex) {
// deal with this specific circumstance
}
finally {
// do some common code
}
Without this, you would be left trying to do a catch-all type of exception.
If however a catch-all will do, then class hierarchy still means that you can still catch all exceptions using catch(Exception ex) {}
.
Providing error-specific information, allowing more fine-grained exception handlers.
Yes. The big advantage is that it allows you to throw and exceptions that mean what you want them to mean. If you reuse an existing exception, any piece of your code that catches the exception has to deal with possibility that the actual exception wasn't thrown by your code, but by some other library party code. This tends to make error handling more flakey.
With a custom exception, you can tell your callers that a specific kind of error has happened, rather than just an error. And the callers may thus do sothing specific to this kind of error.
Let's use a car analogy : do you prefer your car to refuse starting with a unique red light blinking, or with a dedicated "gas tank empty" light blinking?
throw new Exception("I won't start because I'm out of gas")
–
Resplendence In my view the main motivation for custom exceptions is to achieve a better modeling of your application domain. When designing classes you spend a great deal of effort to name objects and assign their responsibilities. I think that at this point some effort for considering possible error conditions is a good investment. For example, when digging deeper, clients can often give you some common examples that have to be handled (e.g. invalid data, violation of logical constraints, unreliable sensors, etc.). As a result you will have code that is easier to understand and modify. The application specific errors are nicely separated and the handling of additional errors can be easily achieved.
Another point is that it might provide better abstraction for different parts of the system. For example, if you really anticipate that the implementation of persistence part will change in future, then it is far better to use custom exceptions in its API. Otherwise you will have later a lot of fun dealing with SQLException or SAXExceptions in many different places :-)
© 2022 - 2024 — McMap. All rights reserved.