What should you do?

Checked Java exceptions sometimes make you catch a checked exception that you think will never be thrown. Best practice dictates that you wrap this in an uncontrolled exception and throw it just in case. What class of exceptions do you wrap in this case?

What exception would you throw in the case "// never happened"?

+6
java exception
source share
9 answers

I don't have one yet, but I would do it like this:

  • if it extends Error , and not RuntimeException, as then it is less likely to be caught by an error handler that is not prepared for it;
  • wrap it in some static utility calls:

Example:

public static void doAssert(boolean result, String reason) { if ( !result ) throw new OMGWereDoomedError("Assertion failed: " + reason + " ."); } 

I prefer this shell, because then I can change the implementation if necessary.

In addition, I am not a supporter of the use of claims, since I do not always control the runtime flags, but I want the execution to stop abruptly if the system was compromised.

Consider this:

  • User display Sorry, we are unable to process your payment page;
  • confirmation of the result to the user, even if the system is in an undefined state.

What would you choose?

+3
source share

For example, my caveat is character conversion in UTF-8 bytes

 String.getBytes("UTF-8"); 

And the need to always wrap it in try-catch, as this is a common method. Since UTF-8 could be considered standard, I would expect a direct direct call to t12 as a string, similar to the JNI GetStringUTF8Bytes method.

Of course, it can be replaced by the constant constant Charset.UTF8, for example, and use getBytes () with this.

The second annoyance for me is the need to wrap close() in a try-catch. Maybe I’m not so experienced, but I can’t understand how this helps when you already have problems with the stream, and you can’t even calmly close it forever. You almost simply registered and ignored the exception.

Apparently, you can use the suppressor method call to do just that.

There are also trivial conversions of the value chain when you are 100% sure that the string is a valid value when you checked it by checking the regular expression or schema.

+2
source share

I am using AssertionError with a string. It should never be.

+2
source share

Any class that is a RuntimeException or its descendant should not be placed in a throws method clause. Therefore, if you do not want someone to deal with this because this should not have happened, use this.

This makes sense for things like “Cannot connect to the database” because if your database is unavailable, your application will not work. This way you throw a Runtime exception and the person starting the application sees a problem in it. But you do not need to declare it in your throws to the level of the call stack.

+1
source share

ImpossibleHappenedException

+1
source share

Conditions that should never occur if the code is not correct should be checked with statements. This way you can disable them in production assemblies.

But I do not like to leave a simple assert false; , and I don’t want to write stupid messages either, such as a case of a default in the switch that should never fire, x should be unnecessary, * it should never happen. ", but it is. Whatever it is. Besides doesn’t it sound like a little whining when you see a message complaining that something should not be? Also I would not like to have tons of these messages in the executable file, as they are usually never used, and each of them only relevant for a small function of thousands of functions.

So i like

  assert false : "Programming Error" 

A programming error is what prevented the application from working so that it is fully consistent with the situation.

  switch (x) { case GO_FORWARD: ... break case BUY_SWORD; ... break default: assert false : "Programming Error" } /* at this point, we have already had checked that we have the money */ try { buy_sword(); buy_elixir(); } catch (InsufficientFunds) { /* if the code was correct, the funds would be sufficient so this event means the code is broken. Telling the user something like "Funds should be sufficient" won't be helpful to the user, so we put in a generic error message */ throw new AssertionError("Programming Error"); } 

If you want to constantly perform these checks, instead

  assert false : "Programming Error" assert expr : "Programming Error" 

do

  if (! expr) throw new Exception("Programming Error") 

or even get a ProgrammingError exception class.

+1
source share

I like "Reached Unreachable Code"

+1
source share

This applies more to the original version of the question before the question is edited specifically to check for noted exceptions.

Disclaimer: I originally posted a snippet of this comment in the question because the question was closed at this point in time.

 int salary = ...; if (salary == 0) { // ... } else if (salary < 0) { // ... } else if (salary > 0) { // ... } else { throw new IllegalStateException("The fundamental mathematics of the universe have been compromised"); } 
+1
source share

An error should never happen more than an exception. An exception describes a situation that you can possibly handle. How do you want to deal with surprise? And since this is an assertion that this particular exception should never occur, I would use AssertionError .

+1
source share

All Articles