Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
For example if I was making an image API in Java would it be more beneficial to have multiple exception classes such as ImageSizeException, ImageFormatException or have a single exception class such as ImageException which has a string, enum ect for what kind of exception it is.
As a secondary question if you use multiple exception classes would it be more acceptable to group them in an exceptions package or with the classes that would throw them
You should certainly have a base exception, ImageException, which is the superclass of any others. Like Java has IOException.
Personally I would prefer one Exception with an Enum or int to subtype, but typical Java style is to have a lot of Exception subclasses. Your should probably follow convention.
It's Better to have many exceptions.it will be easier debugging when your code is huge,and you could have ImageException as the superclassfor all these exceptions.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 months ago.
Improve this question
While using streams, I learned how to handle exceptions using functional interfaces and wrapper functions.
If so, what is the best way to structure the package?
Is it defined together in the class where stream is used? Or should I create a separate common class and define the wrapper function in it?
Thanks!
I think it depends. If you have one instance of using this technique, then it probably makes sense to simply use an functional interface and a wrap function that are part of the class which utilizes it. If you are using this same pattern in several places (and they all have the same function interface signature) then you can define the functional interface at the package level and put the wrap function in a utility class.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
after the introduction of annotation, why not java replaced Serializable with #Serializable annotation
Annotations are not inherited. The Serializable interface is inherited. This means that not only would be break compatibility to remove the interface, it might not be possible to replace it with an annotation as the behaviour is not the same.
Another difference is you can write
void method(Serializable s)
but you cannot do the same for an annotation, although ObjectOutputStream.writeObject takes an Object in any case.
I think mostly because it would mean that a lot of old code that doesn't use the annotation would start giving compiler warnings all over the place.
Serializable is not the only thing that could have been deprecated, but for the sake of backwards compatibility hasn't.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have common methods written in different classes, making code duplicate and now to avoid this duplicacy, I am thinking of 2 approaches:
Make static methods in some util class and call them, or
Make a super class and write all these methods in super class and extend each class with this super class.
Definitely, with approach 2, I will loose the ability to extend my class further. So I am thinking to go with approach 1.
Can you please help me in identifying which approach is good and also in suggesting better approach, if you have any?
Creating Utility packages and/or class is a common solution to this problem. Apache Commons is a prime example. I would favour approach 1
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Hello everyone I would like to know your preference regarding naming java packages. As in do you prefer com.example.controllers (since it contains a group of controllers) or com.example.controller ?
Also if you have an interface named Shape do you prefer to name it IShape or just Shape?
Based on standard package names, it should be singular :
java.util.function contains multiple functional interfaces.
java.util.stream contains multiple Stream interfaces and classes.
Etc...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm writing a class in java (for android) that accepts an org.w3c.dom.Element as input and provides some business logic functions around it. In one of those functions, I call getElementsByTagName and expect only 1 element in the returned NodeList. If there's more than 1 element, it means the Element I got is jacked up somehow and I need to throw an exception. Is there an exception already included in JDK7 which is made to accurately represent this problem?
You could make the method throw an IllegalArgumentException:
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
Or you could write your own Exception class which may be a better solution if you're following this exception-driven approach.
Note however that using exceptions for flow control can cause lower performance.
It's best to define your own Exception, as none of the Java defined exceptions quite fit the case you're experiencing, and it doesn't seem that people outside your organization need to understand the thrown exception.