zoukankan      html  css  js  c++  java
  • trycatchfinally(C# 简单学习)

    catchfinally 一起使用的常见方式是:在 try 块中获取并使用资源,在 catch 块中处理异常情况,并在 finally 块中释放资源。这个好处就是防止有异常的出现,它可以处理异常。在考虑周全的时候应该要用上 

    例子1:

    OleDbConnection Conn=dbcen.Acce_Conn();//用方法SqlConnection 定义数据库连接
    try                                  //尝试打开数据库
       {
        Conn.Open();
        Response.Write("<script>alert('DB连接成功....')</script>");
       }
       catch(Exception M)
       {
        Response.Write(M.Message);
       }
       finally
       {
        Conn.Close();
       }

    例子2:在抓气象局的天气情况代码中

    声明:

    using System.Net;
    using System.Text; 

     protected void Page_Load(object sender, EventArgs e)
        {

            //抓天气情况
               string strResponse = GetPageData("http://www.121.com/weathe.jsp");
                string strRef2 = @"MARQUEE"; //需要用到的判断用的关键字

                if (strResponse.Length > 50)//判断读取天气url是否成功
                {  成功就输出要得到的数据

                }

        }

    private static string GetPageData(string url)   //自定义函数取得url内的数据
        {
            if (url == null || url.Trim() == "")
                return null;
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            try
            {
                Byte[] pageData = wc.DownloadData(url);
                return Encoding.Default.GetString(pageData); ;//.ASCII.GetString
            }
            catch (Exception M)
            {
                return "区气象局资料整理中,天气消息稍后发布,敬请关注";
            }
            finally
            {               
            }     
         }

  • 相关阅读:
    min.js文件 反压缩查看
    Simple HTML DOM解析器 使用php解析网页字符串进行dom操作
    使用clipboard.js实现页面内容复制到剪贴板
    php7微信支付回调失败
    微信卡券开发
    Windows内存性能分析(一)内存泄漏
    LoadRunner添加检查点
    LoadRunner中两种录制模式的区别
    Jmeter参数化_CSV Data Set Config
    APTM敏捷性能测试模型
  • 原文地址:https://www.cnblogs.com/pyman/p/1624656.html
Copyright © 2011-2022 走看看