- 1 Relevancy
- keep the exceptions you throw relevant to the interface.
- 2 Encapsulation
- throws only exceptions that are relevant to the interface
- 3 Reason
- The exceptions must always say what caused them=>To create a generic exception and describe what caused it
- 4 Exception names
- My advice is to avoid using the
java.lang.Exception as it is too generic and gives too little information about what went wrong.
- 5 Balance what you catch
} catch (ClassCastException e1) {
...
} catch (FileNotFoundException e) {
...
} catch (IOException e) {
...
}
- 6 Scoping
- How long should a
try block be?
- 7 Use Finally
} finally {
try {
output.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
- 8 Throw only Exceptions
- 9 Throw early catch late
- you should throw an exception as soon as you can and catch it late, wait until you have all the information to handle it properly.
- 10 Add validation
if ( null == variable || variable.isEmpty()){
throw new blahException("the variable cannot be null at this point");
}
沒有留言:
張貼留言