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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:09:49+00:00 2026-05-17T22:09:49+00:00

My question is about throw and exception bubbling. I was searching around about file

  • 0

My question is about throw and exception bubbling. I was searching around about file locking and C# and I tried messing around with someone’s code which made me question how much I understood about throw and exception bubbling.

Here is the link to the thread.

public class FileManager
{
private string _fileName;
private int _numberOfTries;

private int _timeIntervalBetweenTries;

private FileStream GetStream(FileAccess fileAccess)
{
    var tries = 0;
    while (true)
    {
        try
        {
            return File.Open(_fileName, FileMode.Open, fileAccess, Fileshare.None); 
        }
        catch (IOException e)
        {
            if (!IsFileLocked(e))
                throw;
            if (++tries > _numberOfTries)
                throw new MyCustomException("The file is locked too long: " + e.Message, e);
            Thread.Sleep(_timeIntervalBetweenTries);
        }
    }
}

private static bool IsFileLocked(IOException exception)
{
    int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
    return errorCode == 32 || errorCode == 33;
}

// other code

}

In the code above, if IsFileLocked returns false, throw rethrows an IOException. To my understanding, I thought that now that IOException would bubble up the stack trace. This means, to me at least, that when that throw occurs, it should caught again by the catch(IOException e) again (the one obviously in the GetStream method). After all, the stacktrace would be something like:

GetStream (handling IOExceptions)
IsFileLocked
throw
Exception would be handled in GetStream

However, when I run this, it seems to not bother bubbling up but rather halts my application. Am I missing something here?

Side note: Just so I get the difference between throw and throw ex, if throw ex had been used, the exception would have been shown to have originated from GetStream and then bubble up to no where right? I know there is the same question on here, I just want to reaffirm to myself in this example that this is true.

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-05-17T22:09:50+00:00Added an answer on May 17, 2026 at 10:09 pm

    An exception thrown (or rethrown) from a catch block is not handled by that same catch block, but by next exception handler up the chain. If there is no other handler (and there is no global exception handler in place to gracefully handle the exception), the application will terminate.

    Regarding your side note, throw preserves the call stack on the exception, but an exception rethrown by throw ex will appear to have originated in GetStream().

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

Sidebar

Related Questions

No related questions found

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.