⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmcommonexceptions.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;
using System.Net.Sockets;

namespace CodeForChapter4cs
{
  public partial class frmCommonExceptions : Form
  {
    public frmCommonExceptions()
    {
      InitializeComponent();
    }

    private void menuItem1_Click(object sender, EventArgs e)
    {
      label1.Text = "..";
      try
      {
        // Create a web request for a site.
        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(textBox1.Text);

        // Get the associated response for the above request.
        HttpWebResponse rsp = (HttpWebResponse)wr.GetResponse();
        label1.Text = rsp.StatusCode.ToString();
        rsp.Close();
      }
      catch (WebException ex)
      {
        Debug.WriteLine("This program is expected to throw WebException on successful run." +
                            "\n\nException Message ");
        MessageBox.Show(ex.Message);

        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
          if (ex.Response != null)
          {
            //label1.Text = ((HttpWebResponse)ex.Response).StatusCode.ToString();
            Debug.WriteLine("Response is null");
            return;
          }
          Debug.WriteLine("Status Code : " + ((HttpWebResponse)ex.Response).StatusCode);
          Debug.WriteLine("Status Description : " + ((HttpWebResponse)ex.Response).StatusDescription);
        }
        else
        {
          Debug.WriteLine(ex.Status.ToString());
        }

        Debug.WriteLine(ex.InnerException.ToString() ?? "was null");
        Debug.WriteLine(null ?? "was null");
      }
      catch (Exception ex2)
      {
        Debug.WriteLine(ex2.Message);
      }
    }

private void menuItem2_Click(object sender, EventArgs e)
{
  try
  {
    IPAddress ipAddress = this.GetIpAddressSomehow();

    if (ipAddress == null)
    {
      MessageBox.Show("Sorry could not get ip address.");
      return;
    }

    int port;
    try
    {
      port = Convert.ToInt32(textBox1.Text);
    }
    catch (FormatException)
    {
      MessageBox.Show("Please provide a valid port number.");
      return;
    }

    IPEndPoint ipe = new IPEndPoint(ipAddress, port);

    try
    {
      this.ConnectToThis(ipe);
    }
    catch (CantConnectException ex)
    {
      LogException(ex);
      MessageBox.Show("Sorry, could not connect");
    }
  }
  catch (Exception ex)
  {
    LogException(ex);
    MessageBox.Show("Sorry, an unexpected error has occured. Please shutdown this application.");
    ExitApplication();
  }
}

    private void ExitApplication()
    {
      throw new Exception("The method or operation is not implemented.");
    }

    private IPAddress GetIpAddressSomehow()
    {
      //throw new Exception("The method or operation is not implemented.");
    }

    private void ConnectToThis(IPEndPoint ep)
    {

    }


    private bool ConnectToThis2(IPEndPoint ep)
    {
      Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      try
      {
        s.Connect(ep);
        return true;
      }
      catch (SocketException)
      {
        // translate exception to return result
        return false;
      }
      catch (ArgumentNullException)
      {
        // IPEndPoint passed in is null. Pass it up, caller's fault.
        throw;
      }
      catch (Exception ex)
      {
        LogException(ex); //ouw own logging method
        throw;
      }
    }

    private void LogException(Exception ex)
    {
      //throw new Exception("The method or operation is not implemented.");
    }

    public event EventHandler CantConnect;

    private void UpdateTimer(ref System.Threading.Timer tmr)
    {
      try
      {
        tmr.Change(2000, 10000);
      }
      catch (ObjectDisposedException )
      {
        //tmr = new System.Threading.Timer(...);
        tmr.Change(2000, 10000);
      }      
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -