What to do with never happening, useless exceptions?

njzk2

I have this code:

HttpPut put = new HttpPut(url);
try {
    put.setEntity(new StringEntity(body, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
    // That would really not be good
    e1.printStackTrace();
}

On a platform that is known to support that encoding.

The exception is never going to be raised. I will never do anything about it.

The fact that the code is there still suggests that there is a chance that it may happen, and that the rest of the code could be executed in an unreliable state. But it will never. Or if it does, graceful network connections fallback is the last of my problems.

So I have this ugly useless try catch block. What should I do with it?

(In this specific case, there is not much other choice if I want to use StringEntity. String.getBytes for example has a bunch of methods that would accept a Charset object, for instance, avoiding the need to catch the exception, but not StringEntity)

Jon Skeet

I would throw some kind of RuntimeException which indicates that you think this really should never happen. That way:

  • If it ever does happen, you find out about it rather than swallowing it and continuing
  • Your expectation that it really can never happen is clearly documented

You could even create your own subclass of RuntimeException for precisely this:

// https://www.youtube.com/watch?v=OHVjs4aobqs
public class InconceivableException extends RuntimeException {
    public InconceivableException(String message) {
        super(message);
    }

    public InconceivableException(String message, Throwable cause) {
        super(message, cause);
    }
}

You might also want to encapsulate the operation into a separate method, so that you don't get such catch blocks populating your code. For example:

public static HttpPut createHttpPutWithBody(String body) {
    HttpPut put = new HttpPut(url);
    try {
        put.setEntity(new StringEntity(body, "UTF-8"));
        return put;
    } catch (UnsupportedEncodingException e) {
        throw new InconceivableException("You keep using that encoding. "
            + "I do not think it means what you think it means.", e);
    }
}

Then you can call createHttpPutWithBody wherever you'd need to, and keep your main code "catch clean".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do Examples of the Repository Pattern never deal with Database Connection Exceptions?

From Dev

Exceptions to Never Catch

From Java

SFINAE: What is happening here?

From Dev

what is happening in this python code?

From Dev

What is happening in this block queue?

From Dev

What is happening with this border?

From Dev

What is happening in an "If(..||..)" and "If(...&&...)" construct internally?

From Dev

What is happening with the sizeof this struct?

From Dev

What is happening in this block queue?

From Dev

What is happening with this SVG tag?

From Dev

What is happening in this code?

From Dev

What is happening in this closure?

From Dev

What is happening with this reference variable?

From Dev

What is happening in this swift function?

From Dev

What is happening with this code

From Dev

What is happening with this border?

From Dev

What is happening to the year?

From Dev

What is happening in these MULTIOS redirections?

From Dev

What is happening with the sizeof this struct?

From Dev

What is happening in this make clean?

From Dev

What is really happening in this command?

From Dev

What is happening in this recursion?

From Dev

What is happening with this regular expression?

From Dev

What do stack build plan exceptions mean?

From Dev

Initialization of variable was never used, what do i do?

From Dev

I do not know what is happening with my Android Studio, I do not have any component of Google Material

From Java

Do useless values matter in training?

From Dev

Local variable name is never read ? What should i do?

From Dev

What should I do if rpm -e never completes?