Practical use of exceptions c#



  • I have this code, I don't quite understand how to use the exception.

    public static string PostRequest(string Url, string PostData = "", string UserAgent = "", string Referer = "", CookieDictionary Cookies = null, ProxyType ProxyProto = ProxyType.None, string ProxyString = "")
    {
        request.Proxy = Socks5ProxyClient.Parse(ProxyString);
        ...
        try
        {
            HttpResponse response = request.Post(Url, PostData, "application/x-www-form-urlencoded");
            Cookies = response.Cookies;
            response_html = response.ToString();
        } 
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    How am I supposed to know what kind of exception this is to find out what the problem is, either the remote host is inaccessible or the sax north doesn't answer? In all the examples I saw at the time of the exception, there was some logic and function that ended. But I need to do a little differently, if the sax doesn't answer, to dust the request in a while. Usually, I would just do that:

    while(i<3)
    {
        data = PostRequest();
        if(data != null)
            break;
        Thread.Sleep(5000);
    }
    

    But how am I supposed to throw out the exception, let's say, with the code of reply, that the sax server won't answer until the answer comes, or until all three terations go through, try to send a request? Thank you.

    UPD: A little looked at the exceptions in the xNet library, and they work weirdly. There are three types of exceptions xNet.NetException, xNet.ProxyException and another type. But somehow it's always default. I wanted to know whether such an approach could be used in determining the type of exceptions:

    catch (Exception ex)
    {
        //MessageBox.Show(ex.Message);
        //MessageBox.Show(ex.InnerException.ToString());
    
    if(ex.InnerException.ToString().IndexOf("xNet.ProxyException:") &gt; 0 &amp;&amp; ex.InnerException.ToString().IndexOf("Socket") &gt; 0)
    {
        //MessageBox.Show("F");
        throw new xNet.ProxyException();  
    
    }
    
    throw new xNet.NetException();  
    

    }

    I mean, to define the type of exception to the text in it and then to throw it away? I don't think it's right to do this, but differently, I didn't find a way to distinguish them, because the Exception is always throwing away.



  • Based on the fact that the xNet library (this name by the way) uses its own exemptions, it can be done as follows:

    public static string PostRequest(string Url, string PostData = "", string UserAgent = "", string Referer = "", CookieDictionary Cookies = null, ProxyType ProxyProto = ProxyType.None, string ProxyString = "")
    {
        request.Proxy = Socks5ProxyClient.Parse(ProxyString);
        ...
        try
        {
            HttpResponse response = request.Post(Url, PostData, "application/x-www-form-urlencoded");
            Cookies = response.Cookies;
            response_html = response.ToString();
        }
        catch (xNet.Net.HttpException ex)
        {
            // ошибка случилась внутри библиотеки
        }
        catch (Exception ex)
        {
            // все остальные ошибки
        }
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2