Return of the clean catch code

Asked

Viewed 125 times

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!

  • 3

    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.

  • 2
  • 1
  • 1
  • The correct one would be to make a treatment for the exception, for example by putting a comment. xD

  • @Articuno can treat the error using throws or Try catch. What would be your approach to avoid catch without content?

  • 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.

  • 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

  • 2

    @acklay just make it clear that this is funny, some people can take seriously.

  • @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.

  • 1

    @Marlysson I don’t think you understand the joke ;)

  • 2

    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"

  • 1

    @Karanalvespereira your will to modify the block comment catch with the following text: making a feericide by leaving the catch block empty

Show 8 more comments

1 answer

6


A practical example of how to eliminate an exception treatment and simplify code and give more performance:

public static void main(String[] args) {
    String nome = null; //obviamente que é só para ilustrar, ninguém faria isto, certo?
    System.out.println(nome != null && nome.length() > 10 ? "Nome maior que 10 caracteres" : "Nome menor que 10 caracteres");
}

I put in the Github for future reference.

You may wonder about the handling of the database error. This made no sense. If there was anything that could cause a mistake of this kind it would be the case to capture this exception, and not the Exception. Only deal with the exceptions you have to deal with. That is, don’t deal with other, general exceptions, and don’t capture exceptions that can be avoided in the code. What can be avoided and is not can be considered programming error and try-catch does not serve for this, correct the error in your code.

Read us more links presented in the above comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.