开发者

About throw and exception bubbling

开发者 https://www.devze.com 2023-01-23 09:57 出处:网络
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 t

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 i开发者_StackOverflown 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


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().


A plain throw in a catch will not be caught there again. Only exception raised from the try block will be caught. Your exception will continue up the call stack.

You are right that the stack trace might be different for throw and throw ex, but the stack trace is not what determines where the exception is caught, the throw site is.

0

精彩评论

暂无评论...
验证码 换一张
取 消