Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an exception be thrown upwards

I have these two methods I am working on. One saves and one loads. Obviously both need some sort of error handling so I have implemented some "catch all handling". Now the heel of the hunt is, what happens next is contextual to where, during runtime, the error occurs. Because of this I would like to handle the error in the caller, one level above. That way I can have different logic for different situations.

An example would be. If I check the load on first run and it fails I can assume that they memory may have been cleared. But if I try to load during execution I can assume the memory wasn't cleared (by correct means) and something must be up.

    public void SaveToStorage(AccountCollection Collection)
    {
        try
        {
            var storage = IsolatedStorageSettings.ApplicationSettings;
            storage["defaultCollection"] = Collection;
            storage.Save();
        }
        catch (Exception ex)
        {
            // Do something meaningful here
        }
    }

    public AccountCollection LoadFromStorage()
    {
        try
        {
            AccountCollection collection;
            var storage = IsolatedStorageSettings.ApplicationSettings;
            storage.TryGetValue("defaultCollection", out collection);
            return collection;
        }
        catch (Exception ex)
        {
          // Do something meaningful here
        }

        return null;
    }

Essentially I am asking can I throw the error up to the caller but still retain the original error details.

EDIT: Both John and Andrew having given correct answers. Andrew will get the green tick as I would like to do some other generic clean up in the original class.

like image 540
deanvmc Avatar asked Nov 30 '22 04:11

deanvmc


1 Answers

To all the negative remarks about not catching, he stated he wanted to do some unwinding in the method that the exception is thrown, but also continue it up the stack so other methods can perform something. He isn't catching the exception just to throw it again, he wants to perform some action, then throw it again.

catch (Exception ex)
{
    // Do something
    throw;
}

Edit

For some reason I read this as C++, so removed my remarks about copying the exception.

like image 66
Andrew T Finnell Avatar answered Dec 04 '22 22:12

Andrew T Finnell