Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 261787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:27:40+00:00 2026-05-11T22:27:40+00:00

The major difference between a thing that might go wrong and a thing that

  • 0

“The major difference between a thing
that might go wrong and a thing that
cannot possibly go wrong is that when
a thing that cannot possibly go wrong
goes wrong it usually turns out to be
impossible to get at or repair.”
-Douglas Adams

I have an class FileItems. FileItems constructor takes a file, and throws an exception (FileNotFoundException) if the file doesn’t exist. Other methods of that class also involve file operations and thus have the ability throw the FileNotFoundException. I would like to find a better solution. A solution which doesn’t require that other programmers handle all of these extremely unlikely FileNotFoundExceptions.

The facts of the matter:

  1. The file has been checked to exist but the extremely unlikely possibility exists that through some major fault of reality the file might be deleted before this method is called.
  2. Since the probability of 1 happening is extremely unlike and unrecoverable, I would prefer to define an unchecked exception.
  3. The file is has already been found to exist, forcing other programmers to write code and catch the checked FileNotFoundException seems tedious and useless. The program should just fail completely at that point. For example there is always the chance that a computer may catch fire, but no one is insane enough to force other programmers to handle that as a checked exception.
  4. I run into this sort of Exception issue from time to time, and defining custom unchecked exceptions each time I encounter this problem (my old solution) is tiresome and adds to code-bloat.

The code currently looks like this

 public Iterator getFileItemsIterator() {
    try{
        Scanner sc = new Scanner(this.fileWhichIsKnowToExist);
        return new specialFileItemsIterator(sc);        
       } catch (FileNotFoundException e){ //can never happen} 

    return null;
 }

How can I do this better, without defining a custom unchecked FileNotFoundException? Is there some way to cast a checkedException to an uncheckException?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:27:40+00:00Added an answer on May 11, 2026 at 10:27 pm

    The usual pattern to deal with this is exception chaining. You just wrap the FileNotFoundException in a RuntimeException:

    catch(FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    

    This pattern is not only applicable when an Exception cannot occur in the specific situation (such as yours), but also when you have no means or intention to really handle the exception (such as a database link failure).

    Edit: Beware of this similar-looking anti-pattern, which I have seen in the wild far too often:

    catch(FileNotFoundException e) {
        throw new RuntimeException(e.getMessage());
    }
    

    By doing this, you throw away all the important information in the original stacktrace, which will often make problems difficult to track down.

    Another edit: As Thorbjørn Ravn Andersen correctly points out in his response, it doesn’t hurt to state why you’re chaining the exception, either in a comment or, even better, as the exception message:

    catch(FileNotFoundException e) {
        throw new RuntimeException(
            "This should never happen, I know this file exists", e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer toSource() does not work in Internet Explorer or Safari. It… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer A quick Google reveals SMTP proxy. SMTP Proxy server is… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Add a field to the unit test and suppress the… May 11, 2026 at 11:43 pm

Related Questions

At the moment, our application uses the Trident Win32 component, but we want to
Let's share Java based web application architectures! There are lots of different architectures for
I have recently had two telephone interviews where I've been asked about the differences
This is a simplification of the issue (there are lots of ways of doing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.