java - How can I handle an IOException which I know can never be thrown, in a safe and readable manner? -


"The main difference between something that could be wrong and which could not possibly be wrong is It is that when a thing which is probably not wrong, it goes wrong, usually it turns out that it is impossible to get or repair it. "- Daglas Adams

My Has a class file items nearby. FileItems creates a file, and returns an exception (FileNotFoundException) if the file does not exist; Other methods of that class include file operation and thus it has the ability to throw filesyntfoundaxes. I want to find a better solution I

  1. A solution that other programmers do not need to handle these extremely inconsistent FileNotFound exceptions.

    The facts of this matter:

    1. The file exists, but it is not very likely that some major errors of reality have been removed before this method. can go.
    2. Since 1 is extremely contrary and unrecoverable, I like to define an unchecked exception.
    3. The file already exists, other programmers find it difficult and useless to write the code and catch the checked file notfounded exposition. The program should be completely unsuccessful at that point, for example, there is always a chance that the computer can be.
    4. I go from time to time in this type of exception problem, and every time I face this problem (my old solution) is tedious, then custom uncheck defines exceptions and Adds to code-bloat.

    The code currently looks like

      public internet getFileItemsIterator () {scanner sc = new scanner (this fileWhichIsKnowToExist); Return to the New Special Films Eater (SC); } Hold (FileNotFoundException E) {// can never be} return tap; }  

    How can I improve this without defining a custom unchecked FileNotFoundException? Is there a way to check an unwanted exception?

There is a general pattern to deal with this: You only wrap the FileNotFound exception in runtime exceptions:

  hold (FileNotFoundException e) {new runtime exception (e); }  

This result does not apply only when an exception can not occur in a specific situation (such as yours), but when you have no meaning or intention to handle the exception Happens (like a database link failure).

Edit : Be wary of anti-pattern looking similar, which I have seen in the wild many times:

  Hold (FileNotFoundException E) {new runtime exposure (e. Messages); }  

By doing this, you throw all the important information into the original stacktrace, which often makes problems difficult to track down.

Another edit: Like Thorbjørn Ravn Andersen correctly states in your answer, it does not hurt the state, why are you relishing the exception, either In the comment or better, as an exception message:

  hold (FileNotFoundException e) {new runtime exception ("This should never happen, I know that this file exists", E); }  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -