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
            {               
            }     
         }

  • 相关阅读:
    redis 一主二从三哨兵
    java 调用axis2 webservice
    oracle 自增ID
    yum安装命令的使用方法
    SLES 10安装Oracle10gR2笔记
    信息系统集成资质等级评定条件(暂行)
    ionic imgBase64
    IOS 断点下载
    IOS JSON
    citrix更换vcenter后所需改动几张表
  • 原文地址:https://www.cnblogs.com/pyman/p/1624656.html
Copyright © 2011-2022 走看看