views:

71

answers:

2

I created a simple console application using C#. It creates an instance of a class and calls one of the class' methods. This method contains a try-catch block to catch exceptions of the type System.Net.WebException and rethrow it so that the main method can catch it and act appropriately. When I execute the compiled application the exception does not get passed to the main class and the user would never see my custom error message. Instead this screen pops up telling me that there was an unhandled WebException (it's in German but I think it can be recognized anyway ;-)):

alt text

This is the method inside my class named BuffaloRouter:

    public void Login()
    {
        string sUrl = _sUrl + "/somestuff.htm";
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sUrl);
        CredentialCache credCache = new CredentialCache();
        credCache.Add(new Uri(sUrl), "Basic", _credential);
        request.Credentials = credCache;

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
            _sContent = reader.ReadToEnd();

            response.Close();
            receiveStream.Dispose();
            reader.Dispose();

            _parseSessionIds();
        }
        catch (WebException)
        {
            throw;
        }
    }

And this is the method inside my main class:

    private static bool _login()
    {
        _router = new BuffaloRouter(_sIP, new System.Net.NetworkCredential("root", _sPassword));

        try
        {
            Console.WriteLine("Login...");
            _router.Login();
            return true;
        }
        catch (System.Net.WebException)
        {
            _showErrorMessage("Could not connect to " + _sIP);
            return false;
        }
    }

UPDATE: I feel more than a little embarrassed and would rather not talk about it. But like a few times before I didn't relly look at what I was doing ;-) The method inside the main class was not even invoked when I was running the app. The one that was invoked didn't have a try-catch block so that the exception thrown inside my class' method made the app do what it was supposed to, i.e. CRASH!! I'm stupid, sorry for wasting everone's time.

+4  A: 

If all you're doing is rethrowing the exception, then you don't need to catch it in the first place. Just remove the try...catch from your Login method.

Having done that temporarily edit your code to catch the general exception and debug it to find out what exception is actually being raised. Having done that edit the code again to catch that exception (and that exception alone).

ChrisF
+1  A: 

As ChrisF has indicated you don't need to rethrow the error in the Login method. If your catch block is not running in the _login method, then I would guess your not catching the right exception.

Are you positive your getting a System.Net.WebException

JoshBerke