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

The Archive Base Latest Questions

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

static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException { BufferedReader br = new BufferedReader(new FileReader(path)); try

  • 0
static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {
  BufferedReader br = new BufferedReader(new FileReader(path));
  try {
    return br.readLine();
  } finally {
    if (br != null) br.close();
  }
}

if the methods readLine and close both throw exceptions, then the method readFirstLineFromFileWithFinallyBlock throws the exception thrown from the finally block; the exception thrown from the try block is suppressed. Why is the behaviour as such? Why is the exception from try block supressed?

  • 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-26T04:02:15+00:00Added an answer on May 26, 2026 at 4:02 am

    Only exceptions within the try portion will be handled. Anything outside this (including the finally section isn’t covered by a try and as such, Exceptions aren’t handled.

    If you want to catch/suppress the exception inside the finally block, you need to wrap if (br != null) br.close(); with it’s own try/catch block, like this:

    ...
    } finally {
      try {
        if (br != null) br.close();
      } catch (Exception e) {
        // whatever handling
      }
    }
    ...
    

    Also, the exception from the try block is suppressed because that’s the behavior of a try block – to try something and give you a chance to recover. Since you don’t catch any exceptions after your try block, no code is run in response to it.

    Then, whether or not any exception is thrown, the finally block is executed. If it throws an exception, and because it’s not inside a try/catch block of its own, its Exception is propagated out the method, and to the calling method.

    To take the example from your comment below, as of Java 7, you’ll want to refer to the documentation outlined here, and focus on the second-to-last section entitled “Supressed Exceptions”, which basically says that multiple exceptions can be thrown out of a try block, up to one exception per declared resource.

    As far as what happens if the resource declaration itself throws an exception, I don’t have JDK7 installed, so I’m not sure. Why not put the following code in a test project (exactly as it appears, with a bogus path), see what happens, and then tell us what the result it for everyone’s benefit:

    try (BufferedReader br = new BufferedReader(new FileReader("a totally invalid path"))) {
      return br.readLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static string SerializeObject<T>(T obj) { try { string xmlString = null; MemoryStream memoryStream
I have the next 2 methods: public static string[] Method1(CustomObj co) { return new
look at these code: public static String get(String url, Properties parameters) throws MalformedURLException, IOException{
public static String getElementXpath(DOMElement elt){ String path = ; for (; elt != null;
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
public static string GetCurrentEmployeeName() { return db.Employees.SingleOrDefault( c => c.NTUserName == CurrentUsername() ).Last_First; }
public static string TimeLine2(this HtmlHelper helper, string myString2) { StringBuilder myString3 = new StringBuilder();
Code Behind: [WebMethod] public static string emp() { return BlaBla; } Aspx Page: $(document).ready(function()
I have the C# method private static string TypeNameLower(object o) { return o.GetType().Name.ToLower(); }
public static string GetUa(HttpRequest hr) { try { string visitorBrowser = hr.UserAgent.ToString(); string originalBrowser

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.