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 6744155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:02:59+00:00 2026-05-26T12:02:59+00:00

I have a simple servlet where I write to a file if it has

  • 0

I have a simple servlet where I write to a file if it has a queryparameter ‘hello’, and since this is a test I want to display the error the the webpage also.

IntelliJ is complaining that I am not catching the IOException, not sure what’s wrong:

private static void WriteToFile(String filePath, String fileName, String fileData) {

    FileWriter writer = null;

    try {
        writer = new FileWriter(fileName);
        writer.write(fileData);
    } catch(IOException ex) {


    } finally {
        if(writer != null) {
            writer.close();
        }

    }


}

Also, in my exception, I noticed on the web most people write:

How can I output the error to the web page?

  • 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-26T12:02:59+00:00Added an answer on May 26, 2026 at 12:02 pm

    You’re not catching IOException when you call writer.close(); in the finally block.

    You’re also completely swallowing any IOException thrown in the main code, which is a really bad idea. If something’s goes wrong, you’ll have no idea what’s happening.

    I would personally suggest that you let that method throw the exception to the caller:

    private static void writeToFile(String filePath, String fileName,
                                    String fileData) throws IOException {
      FileWriter writer = new FileWriter(fileName);
      try {    
        writer.write(fileData);
      } finally {
        writer.close();
      }
    }
    

    Note that if the try block throws an exception and the finally block does, you’ll effectively “lose” the original exception. You may want to suppress exceptions throw when closing.

    Or just use Guava which makes all of this simpler anyway with its Files class.

    Or if you’re using Java 7, you could use a try-with-resources statement.

    (I note that you’re ignoring filePath by the way – why?)

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

Sidebar

Related Questions

I have a simple jsp/servlet application and I want to add AJAX feature to
I have a simple GWT project in Eclipse, it has a servlet that calls
I have a simple servlet say com.test.HelloWorld.class then I create these folders tree: WEB-INF
I have created a simple Servlet that I want to deploy in Jetty 7.2.
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package in which On Error event handler I have created
I have simple SSIS package where I import data from flat file into SQL
I have a simple problem: I want to configure an object differently based on
I have a simple (Servlet, JSP, and JSTL) web app whose main functionality is
Let's say I have a simple Login servlet that checks the passed name and

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.