WPF,处理错误和警告消息

时间:2010-11-14 19:46:15

标签: c# exception exception-handling error-handling

我需要提前。我发送HTTP GET请求并解析HTML响应,如果HTML响应(html字符串)包含一些子字符串,我想发送到客户端应用程序,(WPF应用程序)一些错误/警告消息。

在我的解决方案中,如果html字符串包含一些子字符串,我会抛出新的异常,这是愚蠢的,什么解决方案适合这个问题?

代码在这里:

class MyClass
{
//.....

private bool SendRp(string postData)
  {
    bool result = false;

    const string parameters = @"&lok=1&rpI=3";
    string htmlStringResult = HttpPostReq(
      new Uri(string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", PokecUrl.Rp, Account.SessionId, parameters)), postData);
    try
    {
      if (htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("is is empty"))
      {
        throw new ArgumentException("ID is empty!");
      }
      if (htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("id does not exist"))
      {
        throw new ArgumentException("ID does not exist.");
      }
      if (htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("blocked"))
      {
        throw new WebException("Your ID is blocked!");
      }
      if (!htmlStringResult.ToLower(new CultureInfo("sk-Sk")).Contains("message was send"))
      {
        Match match = Regex.Match(htmlStringResult, @"\bhs=\w{15}\b");

        if (match.Success)
        {
          result = true;
        }
        else
        {
          throw new Exception("Some problem"); 
        }

      }
      return result;
    }
    catch (Exception exception)
    {
      throw exception;
    }
  }

}

1 个答案:

答案 0 :(得分:0)

不确定我是否理解这是你的问题。可以在“Web请求者”类上抛出异常,然后在UI类上捕获异常以显示消息。

显示消息的最简单方法是使用MessageBox

MessageBox.Show("Hello MessageBox");