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

  • SEARCH
  • Home
  • 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 819925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:23:24+00:00 2026-05-15T02:23:24+00:00

Wrote up a basic file handler for a Java Homework assignment, and when I

  • 0

Wrote up a basic file handler for a Java Homework assignment, and when I got the assignment back I had some notes about failing to catch a few instances:

  • Buffer from file could have been null.
  • File was not found
  • File stream wasn’t closed

Here is the block of code that is used for opening a file:

/**
 * Create a Filestream, Buffer, and a String to store the Buffer.
 */
FileInputStream fin = null;
BufferedReader buffRead = null;
String loadedString = null;

/** Try to open the file from user input */
try
{
    fin = new FileInputStream(programPath + fileToParse);
    buffRead = new BufferedReader(new InputStreamReader(fin));
    loadedString = buffRead.readLine();
    fin.close();
}
/** Catch the error if we can't open the file */
catch(IOException e)
{
    System.err.println("CRITICAL: Unable to open text file!");
    System.err.println("Exiting!");
    System.exit(-1);
}

The one comment I had from him was that fin.close(); needed to be in a finally block, which I did not have at all. But I thought that the way I have created the try/catch it would have prevented an issue with the file not opening.

Let me be clear on a few things: This is not for a current assignment (not trying to get someone to do my own work), I have already created my project and have been graded on it. I did not fully understand my Professor’s reasoning myself. Finally, I do not have a lot of Java experience, so I was a little confused why my catch wasn’t good enough.

  • 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-15T02:23:25+00:00Added an answer on May 15, 2026 at 2:23 am
    • Buffer from file could have been null.

    The file may be empty. That is, end-of-file is reach upon opening the file. loadedString = buffRead.readLine() would then have returned null.

    Perhaps you should have fixed this by adding something like if (loadedString == null) loadedString = "";

    • File was not found

    As explained in the documentation of the constructor of FileInputStream(String) it may throw a FileNotFoundException. You do catch this in your IOException clause (since FileNotFoundException is an IOException), so it’s fine, but you could perhaps have done:

    } catch (FileNotFoundException fnfe) {
        System.err.println("File not fonud!");
    } catch (IOException ioex {
        System.err.println("Some other error");
    }
    
    • File stream wasn’t closed

    You do call fin.close() which in normal circumstances closes the file stream. Perhaps he means that it’s not always closed. The readLine could potentially throw an IOException in which case the close() is skipped. That’s the reason for having it in a finally clause (which makes sure it gets called no matter what happens in the try-block. (*)


    (*) As @mmyers correctly points out, putting the close() in a finally block will actually not be sufficient since you call System.exit(-1) in the catch-block. If that really is the desired behavior, you could set an error flag in the catch-clause, and exit after the finally-clause if this flag is set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer RegexBuddy will "translate" any regex for you. When fed your… May 16, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer Yes, it's possible using rounded corders gradients border text shadow… May 16, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer I use a macrodef for this: <property environment='env'/> <property name='grails.home'… May 16, 2026 at 5:05 pm

Trending Tags

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

Top Members

Related Questions

I wrote the basic code below and saved to a file called pdf.java. package
I wrote a simple PyGTK script to show some basic process information in a
I'm trying to write a basic Erlang program that reads in a file in
I got the following very basic template: <html> <head> </head> <body> <div> <!-- Using
I've recently been writing some basic command-line programs (I want to keep my skills
We have a requirment which requires to have an Handler that is extended from
Okay, so I'm working on a project where I use a Java program to
My application pushes out a PDF file to a popup (e.g. no menu/toolbar) browser
I have been trying to use an ajax-style file upload component (like the dojox
As a pet-project, I'd like to attempt to implement a basic language of my

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.