Stephan van Hulst wrote:You're approaching the problem from the wrong perspective. Don't declare exceptions because you're using things that may throw exceptions, declare exceptions because they make sense when a user calls your method.
For instance, it would be ridiculous to throw an IOException in a method named calculatePi(int digits), even *if* your method uses I/O operations to retrieve the results.
Campbell Ritchie wrote:You should also consider what is right for your method, decide to do it, and /**write it down in the comments*/
Campbell Ritchie wrote:What did you write in your documentation comments about Exceptions?
Campbell Ritchie wrote:In that case you are condemned to use those exceptions for ever. If you say, “throws StuentException if student is banned, fails to pay the bills or walks across professors' garden,— you have a lot more flexibility.
Look at List#get(). That says in the documentation that it might throw an IndexOutOfBoundsException, so let's look at an implementing class. The same method in ArrayList describes the same Exception, rather than ArrayIndexEtc. If you declared ArrayIndexEtcException, then you are declaring that your implementation uses an array and you are committed to arrays for ever. If you are vague about the Exception you can use it or any of its subtypes and add new conditions meriting Exceptions.
Avor Nadal wrote:It consists in throwing an exception relative to the current context, not too specific, and attaching an enum to it, to be more specific about the cause.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:[pedantic mode]Very few methods return an Exception. This is one. Many methods declare that they might throw an Exception.[/pedantic mode]
Campbell Ritchie wrote:Agree about specific information being added. When one says “cause” about Exceptions one often means a previous Exception which is being chained; many Exceptions can take another Exception as a constructor argument. Examples here: 1 2 3. You can read about Exception Chaining in the Java Tutorials.
Winston Gutkowski wrote:But if you're going to do that, why not simply throw a parent, and include the actual cause? Sounds to me like a solution to a problem that doesn't exist.
Campbell Ritchie wrote:Exceptions aren't parameterised anyway, so why does generics make any difference?
Campbell Ritchie wrote:There is a reason for using the word cause.