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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:36:31+00:00 2026-06-01T05:36:31+00:00

Is it considered bad programming to write a try and catch within a finally

  • 0

Is it considered bad programming to write a try and catch within a finally clause?

I’m having in my main method a fileInputStream which I want to close.
I want to place the .close() in the finally, so it will close no matter what. I don’t want to add a throws declaration to the main method, as it is the main method 😛

    }finally{
        try {
            commandFile.close();
        } catch (IOException e) {
            throwException(e);
        }
    }

is it ok?
Thanks

  • 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-06-01T05:36:32+00:00Added an answer on June 1, 2026 at 5:36 am

    The pattern of needing try/catches in finally methods is unfortunately a recurring pattern in Java 6 and before. I would argue that it actually IS a bad practice, but not one that you can really avoid in Java 6 (see below for Java 7).

    An addition problem is that any new exceptions thrown in the finally block will override exceptions that were thrown before reaching this block.

    In Java 7 there is specifically for the cases where resources need to be closed (the majority of the use cases for try/finally/try/catch constructs) the new try-with-resources construct. This will also capture both the primary and secondary exceptions.

    Using this construct is thus now a best practice in JDK 7 and yes, the code you show is thus a bad practice in Java 7.

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

Sidebar

Related Questions

Is it considered bad practice to have multiple try's in one method and structure
In an MVC3 application, is it considered bad practice to use a try catch
Is having tags inside a jstl tag considered bad form? So for example I
Is it considered bad practice to have multiple views for same URL in MVC,
Is it generally considered bad practice to provide Iterator implementations that are infinite; i.e.
In C, it is considered bad practice to call strlen like this: for (
Why is locking a type considered very bad? For example, lock(typeof(DateTime)) I understand that
In Java classes is it considered good or bad practice to access member fields
Possible Duplicate: Are iframes considered 'bad practice'? While working with web developers, I always
In Unix C programming, is it considered good practice to explicitly close file handles

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.