what's the advantages to define custom exception?
Asked Answered
V

6

7

Any big reasons to define custome exceptions in Java?

Victualer answered 9/3, 2011 at 11:2 Comment(0)
S
14

Two reasons immediately spring to mind:

  1. 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).
  2. 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 an AccountLimitExceededException could contain the current account limit. Extra information in the exception allows you to potentially take a more well-informed response when catching it.
Staggers answered 9/3, 2011 at 11:5 Comment(1)
can you please give an working example instead so that it becomes clearer to me ?Graphomotor
S
3

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) {}.

Sialkot answered 9/3, 2011 at 11:11 Comment(1)
You might want to rename the classes to ExceptionalCircumstance2 and ExceptionalCircumstance3 ;-)Aquatint
L
2

Providing error-specific information, allowing more fine-grained exception handlers.

Lancastrian answered 9/3, 2011 at 11:5 Comment(0)
R
1

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.

Resplendence answered 9/3, 2011 at 11:8 Comment(0)
I
0

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?

Indigestion answered 9/3, 2011 at 11:8 Comment(2)
I know what you are trying to say, but talking about messages is not a good analogy. If it was just messages, then we could all just throw new Exception("I won't start because I'm out of gas")Resplendence
You're right. As always, a car analogy is not a very good analogy. I'll edit my post to avoid talking about messages.Indigestion
C
0

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 :-)

Collocutor answered 9/3, 2011 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.