1
I am reading the book Clean Code of the series of Robert C. Martin. And it was presented a very common situation in my day to day.
public static void main(String[] args) {
String nome = null;
try {
if (nome.length() > 10) {
System.out.println("Nome maior que 10 caracteres");
} else {
System.out.println("Nome menor que 10 caracteres");
}
} catch (Exception e) {
// Não encontro o registro no banco de dados
}
}
Sometimes we perform some operation that the exception is inevitable. In this code for example, when happening the exception in nome.length()
I want the error to be ignored.
Do I leave the comment to describe the reason for the error? Take the comment? Or we can work as another approach?
Remembering that I don’t want to make an exception!
This example exception is not inevitable. Nullpointerexception should be treated whenever possible, even for being simple its treatment. If you do not want to generate an exception, treat the error.
– user28595
Related: What exceptions should I catch in a Try-catch?
– user28595
Related²:How to best treat exceptions in Java?
– user28595
Related³:What is the best method for exceptions?
– user28595
The correct one would be to make a treatment for the exception, for example by putting a comment. xD
– viana
@Articuno can treat the error using throws or Try catch. What would be your approach to avoid catch without content?
– karanalpe
Give a read on the answers of the 3 questions that Linkei, all deal with this same theme, and there are plenty of tips and clarifications on this subject.
– user28595
Great. I’ll do it! @acklay a phrase I saw in the very good book is this: "The use of comments is to compensate for our failure to express ourselves in the code". For that reason I asked the question. hahahha
– karanalpe
@acklay just make it clear that this is funny, some people can take seriously.
– Maniero
@bigown but has a bit of truth, a well explanatory code speaks for itself and would not need the comments, but the comments are inevitable when the implementation demands some inherent complexity of the business rule, in this case being adequate.
– Marlysson
@Marlysson I don’t think you understand the joke ;)
– Maniero
Just to complement you. I saw in one of the posts provided by @Articuno a funny and very good sentence that guided me about my question: "A fairy dies every time a programmer leaves an empty catch"
– karanalpe
@Karanalvespereira your will to modify the block comment
catch
with the following text: making a feericide by leaving the catch block empty– Jefferson Quesado