Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About throw and exception bubbling

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

like image 911
Ilya Avatar asked Dec 21 '22 21:12

Ilya


2 Answers

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

like image 187
Phil Hunt Avatar answered Dec 24 '22 09:12

Phil Hunt


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.

like image 31
Ned Batchelder Avatar answered Dec 24 '22 10:12

Ned Batchelder