संभव डुप्लिकेट:
Best practices for exception management in JAVA or C#स्वच्छ कोड बनाने के लिए प्रयास ब्लॉक को पकड़ने के लिए सबसे अच्छा अभ्यास क्या है?
मैं stackoverflow पर a question आज पहले पढ़ा है और यह मेरे अपवाद से निपटने के लिए सबसे अच्छा अभ्यास है क्या बारे में सोचते हैं बनाया है।
तो, मेरे सवाल का है सबसे अच्छा अभ्यासके अपवाद को संभालने के लिए है क्या स्वच्छ और उच्च गुणवत्ता कोड का उत्पादन।
यहाँ मेरी कोड है, मुझे लगता है कि यह सीधे आगे चुप है, लेकिन मुझे पता है कि अगर मैं गलत या स्पष्ट नहीं कर रहा हूँ तो कृपया! मैंने विधियों में टेस्टेबिलिटी और समान अमूर्त स्तर को ध्यान में रखने की कोशिश की है।
हर रचनात्मक टिप्पणी का स्वागत किया है। :)
import java.awt.Point;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
/**
* <p>This is a dummy code.</p>
* The aim is present the best practice on exception separation and handling.
*/
public class ExceptionHandlingDemo {
// System.out is not a good practice. Using logger is good for testing too (can be checked if the expected error messages are produced).
private Logger logger = LoggerFactory.getLogger(ExceptionHandlingDemo.class);
// instance of cannot work with List<Point>
private interface PointList extends List<Point> {}
/**
* The method that loads a list of points from a file.
* @param path - The path and the name of the file to be loaded.
* Precondition: path cannot be {@code null}. In such case {@link NullPointerException} will be thrown.
* Postcondition: if the file don't exist, some IOException occurs or the file doesn't contain the list the returned object is {@code null}.
* */
/* (Google throws NullpointerExceptio) since it is not forbidden for the developers to throw it. I know this is arguable but this is out of topic for now. */
public List<Point> loadPointList(final String path) {
Preconditions.checkNotNull(path, "The path of the file cannot be null");
List<Point> pointListToReturn = null;
ObjectInputStream in = null;
try {
in = openObjectInputStream(path);
pointListToReturn = readPointList(in);
} catch (final Throwable throwable) {
handleException(throwable);
} finally {
close(in);
}
return pointListToReturn;
}
/*======== Private helper methods by now ========*/
private ObjectInputStream openObjectInputStream(final String filename) throws FileNotFoundException, IOException {
return new ObjectInputStream(new FileInputStream(filename));
}
private List<Point> readPointList(final ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
final Object object = objectInputStream.readObject();
List<Point> ret = null;
if (object instanceof PointList) {
ret = (PointList) object;
}
return ret;
}
private void handleException(final Throwable throwable) {
// I don't know the best practice here ...
logger.error(throwable.toString());
}
private void close(final Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
logger.error("Failed closing: %s", closeable);
}
}
}
/*======== Getters and setters by now. ========*/
// ...
/**
* @param args
*/
public static void main(String[] args) {
ExceptionHandlingDemo test = new ExceptionHandlingDemo();
test.loadPointList("test-filename.ext");
}
}
संपादित:
क्या मैं से बचने के लिए एक दूसरे के बाद पकड़ के मामलों की बहुत लिख रहा है चाहते हैं ...
@xscape: मैं इसे निश्चित रूप से पढ़ रहा हूं, धन्यवाद। – uthomas
एक साइड नोट के रूप में, मैं अनुशंसा करता हूं कि आप अपने निजी सहायक विधियों को 'स्थैतिक' चिह्नित करें, क्योंकि वे यही हैं: वे इनपुट लेते हैं और उन इनपुटों के आधार पर आउटपुट का उत्पादन करते हैं। जब मैंने आपकी विधि घोषणा पर कोई 'स्थिर' कीवर्ड नहीं देखा, तो मुझे यह जांचने के लिए मजबूर होना पड़ा कि क्या विधियों ने वास्तव में किसी भी राज्य का उपयोग नहीं किया है, जो खराब है, क्योंकि कोड मुझे इसे पढ़ने के लिए महत्वपूर्ण कुछ पर ध्यान केंद्रित करने के लिए मजबूर नहीं करता है। यह प्रश्न यह बताता है कि: http://stackoverflow.com/questions/3346764/should-java-methods-be-static-by-default –
@ ब्रूनो रीइस: धन्यवाद, मैं इसे पढ़ूंगा। – uthomas