Question

Lets say i have a method in which I perform following operation its just a test method just to show what i actually need.

 public void ExceptionOccured()
        {
            try
            {
                WebClient client = new WebClient();
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                Stream _data = client.OpenRead(_apiCallArgs);
                // if exception occurs in Open read method

                WebRequest request = WebRequest.Create("http://www.contoso.com/default.html");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                // if exception occurs in get response method

                int a = Convert.ToInt32("anyvariable");
                // if exception occurs here
            }
            catch (Exception ex)
            {
                // how can i find that who occurred the exception either its is occurred by webclient or httprequest or by coversion ?
            }


        }

how can i find out that who occurred the exception either its is occurred by webclient or httprequest or by coversion ?

Was it helpful?

Solution

    public void ExceptionOccured()
    {
        try
        {
            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            Stream _data = client.OpenRead(_apiCallArgs);
            // if exception occurs in Open read method

            WebRequest request = WebRequest.Create("http://www.contoso.com/default.html");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // if exception occurs in get response method

            int a = Convert.ToInt32("anyvariable");
            // if exception occurs here
        }
        catch (Exception ex)
        {
           Type exType = ex.GetBaseException().GetType();
        }


    }

OR

    public void ExceptionOccured()
    {
        try
        {
            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            Stream _data = client.OpenRead(_apiCallArgs);
            // if exception occurs in Open read method

            WebRequest request = WebRequest.Create("http://www.contoso.com/default.html");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // if exception occurs in get response method

            int a = Convert.ToInt32("anyvariable");
            // if exception occurs here
        }
        catch (WebException exhttp)
        {
             //Web exception directally catch by this block
        }
        catch (Exception ex)
        {

        }


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top