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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:54:27+00:00 2026-05-23T13:54:27+00:00

How to correctly let an exception to bubble up? If I use Try-Catch when

  • 0

How to correctly let an exception to bubble up?
If I use Try-Catch when calling a method, is just throwing an exception inside a method like not trying to catch it at all?
For illustration: Are these approaches do the same work?

Example 1:

try
{
   MyFileHandlingMethod();             
}
catch (IOException ex)
{
   string recfilepath = "...";
   string rectoadd = "RecDateTime=" + DateTime.Now.ToString()+ ...+ex.Message.ToString();
   File.AppendAllText(recfilepath, rectoadd);
}
catch (exception)
{
   throw;
}
...
MyFileHandlingMethod()
{
   ...
   TextReader tr2 = new StreamReader(nfilepath);
   resultN = tr2.ReadLine();
   tr2.Close();  
   ...
}

Example 2:

try
{
   MyFileHandlingMethod();             
}
catch (IOException ex)
{
   string recfilepath = "...";
   string rectoadd = "RecDateTime=" + DateTime.Now.ToString()+ ...+ex.Message.ToString();
   File.AppendAllText(recfilepath, rectoadd);
}
catch (exception)
{
   throw;
}
...
MyFileHandlingMethod()
{
   ...
     try
     {
        TextReader tr2 = new StreamReader(nfilepath);
        resultN = tr2.ReadLine();
        tr2.Close();              
     }
     catch (Exception)
     {
        throw;     
     }       
   ...
}
  • 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-23T13:54:28+00:00Added an answer on May 23, 2026 at 1:54 pm

    Yes, those 2 approaches have almost the same effect; rethrowing will unwind the stack of the exception – meaning the stack frames “below” the method where the throw; will be discarded. They’ll still be in the stack trace, but you won’t be able to access their local variables in the debugger unless you break on thrown exceptions.

    A catch/throw block like the one below where you don’t do anything with the exception (like logging), is useless:

     catch (Exception)
     {
        throw;     
     } 
    

    Remove it to clean up, in both your samples. In general, avoid entering a catch block if possible


    And your method has another exception related problem, it does not free resources properly. The tr2.Close(); belongs in a finally clause but it’s much easier to let the compiler handle that with a using() {} block :

    void MyFileHandlingMethod()
    {
       ...
       using (TextReader tr2 = new StreamReader(nfilepath))
       {
         resultN = tr2.ReadLine();         
       } //tr2.Dispose() inserted automatically here        
       ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a fairly straightforward question, but I couldn't find this particular use-case
I think swept means determining if objects will collide at some point, not just
Let me try to clarify my question, I wasn't sure how to word the
In the following let's say zip codes I am trying to exclude the 33333-
I'm trying to write a Compare method to compare properties in some POCOs using
I have an F# exception that it is not being caught in the correct
I've used exceptions in Java and like the way it won't let you call
Possible Duplicate: C++ STL stack question: Why does pop() not throw an exception if
First let me explain how I currently handle validation, say, for an IPv4 address:
Let's say I have a web page that currently accepts a single ID value

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.