A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 何荣智 黑马帝   /  2011-10-29 12:12  /  3573 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有谁能具体讲一下 这个的用法啊  最好是有代码的啊  

11 个回复

倒序浏览
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Text.RegularExpressions;

  9. namespace ConsoleApplication
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string a = Console.ReadLine();
  16.             try
  17.             {
  18.                 int b = int.Parse(a);
  19.             }
  20.             catch
  21.             {
  22.                 Console.WriteLine("a不是整型");
  23.             }
  24.             finally
  25.             {
  26.                 Console.WriteLine("不管成功失败都显示");
  27.             }
  28.         }
  29.     }
  30.    

  31. }
复制代码
回复 使用道具 举报
陈涛 黑马帝 2011-10-29 16:34:54
藤椅
本帖最后由 陈涛 于 2011-10-29 16:38 编辑
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace 数字相加
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.       
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }

  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             //try
  21.             //{
  22.             //    int sum=0;
  23.             //    int str1 = Convert.ToInt32(textBox1.Text);
  24.             //    int str2 = Convert.ToInt32(textBox2.Text);
  25.                
  26.             //    if (str1 > str2)
  27.             //    {
  28.             //        MessageBox.Show("输入的第一个数字必须小于第二个数字!");
  29.             //        return;
  30.             //    }
  31.             //    for (;str1 <=str2; str1++)
  32.             //    {
  33.             //        sum += str1;
  34.             //    }
  35.             //    label4.Text = sum.ToString() ;
  36.                
  37.             //}
  38.             //catch (Exception)
  39.             //{

  40.             //    MessageBox.Show("请输入正确的数字!");
  41.             //}
  42.             string str1 = textBox1.Text;
  43.             string str2 = textBox2.Text;
  44.             int sum = 0;
  45.             int i1,i2;
  46.             if (!int.TryParse(str1, out i1))
  47.             {
  48.                 MessageBox.Show("第一个数不是整数!");
  49.                 return;
  50.             }
  51.             if (!int.TryParse(str2, out i2))
  52.             {
  53.                 MessageBox.Show("第二个数不是整数!");
  54.                 return;
  55.             }
  56.             if (i1 > i2)
  57.             {
  58.                 MessageBox.Show("输入的第一个数字必须小于第二个数字!");

  59.                 return;
  60.             }
  61.             for (; i1 <= i2; i1++)
  62.             {
  63.                 sum += i1;
  64.                 if (sum == 2)
  65.                 {
  66.                     sum = 0;
  67.                 }
  68.                 sum+= i1;
  69.             }
  70.             label4.Text = sum.ToString();
  71.         }
  72.     }
  73. }
复制代码
这个是那个计算1到100的和, 打了注释的部分用到try catch

try { //执行的代码,其中可能有异常。一旦发现异常,则立即跳到catch执行。否则不会执行catch里面的内容 }

catch { //除非try里面执行代码发生了异常,否则这里的代码不会执行 }

finally { //不管什么情况都会执行,包括try catch 里面用了return ,可以理解为只要执行了try或者catch,就一定会执行 finally }

回复 使用道具 举报
上面的代码已经说的很清楚了,我就针对你的提问说下try{}catch吧。
try{}catch的组合用法,是在一个类跑出异常之后,捕获这个异常通常用的办法。
什么样的时候用try{}catch的组合呢,是在一个类抛出异常之后,所有的类又无法处理的时候,这时候一味的像外抛出,
也不会有什么意义,所以对异常进行捕获处理。
回复 使用道具 举报
楼上各位说的都特别好,不过好像另一个问题又出现了,到底什么时候应该捕获异常,什么时候应该抛出异常呢?在C#有没有像java那样把异常作为函数的返回值的?
回复 使用道具 举报
public int Fill(string sql, DataSet dataset, IDataParameter[] param,bool isStoredProcedure)
        {
            try
            {
                if (DBConnector.Connection.State == ConnectionState.Closed)
                {
                    DBConnector.Connection.Open();
                }

                IDbCommand cmd = DBConnector.GetCommand(sql, param);
                //是否是事务
                if (isTranscation)
                {
                    cmd.Transaction = transaction;
                }
                //是否是存储过程
                if (isStoredProcedure)
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                }

                IDbDataAdapter adapter = GetAdapter(cmd);
               
                return adapter.Fill(dataset);
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
                throw e;
            }
            finally
            {
                if (!isTranscation)
                {
                    DBConnector.Connection.Close();
                }
            }
        }

一般情况都是在数据库连接,进行查询以及相关操作时使用;或者在进行类型转换中使用,以及一些复杂逻辑中进行使用,以捕捉异常,处理发生错误问题时,更好更快的找出问题得以解决。

评分

参与人数 1技术分 +2 收起 理由
杨恩锋 + 2

查看全部评分

回复 使用道具 举报
沙铁健 黑马帝 2011-11-1 21:57:26
7#
try{}catch finally在try里执行出错就会跳入catch块里去 这里你可以捕捉异常,finally是不论什么情况都会执行 就想你一般写连接数据库最后 要关闭connection.close()  不过我一般用using catch ^_^

评分

参与人数 1技术分 +1 收起 理由
杨恩锋 + 1

查看全部评分

回复 使用道具 举报
朱勋 黑马帝 2011-11-2 09:10:04
8#
学习了,具体什么情况下抛出异常要根据你编写的程序来定,但是有些情况下是必须有的,比如,在数据库连接、操作等方面。

评分

参与人数 1技术分 +1 收起 理由
杨恩锋 + 1

查看全部评分

回复 使用道具 举报
宋天琪 黑马帝 2011-11-4 21:23:42
9#
try里边写代码,把可能出现错误的代码都放try里边,catch用于捕获异常。。。
回复 使用道具 举报
张大福 黑马帝 2011-12-13 21:44:17
10#
提示: 作者被禁止或删除 内容自动屏蔽
回复 使用道具 举报
贾宁 黑马帝 2011-12-16 09:15:28
11#
   Console.WriteLine("请输入一个数字:");
            int number = 0;
            bool isnumber = false;
            while(isnumber==false)
            {
            try
            {
                number = Convert.ToInt32(Console.ReadLine());
                isnumber = true;
            }
            catch
            {
                Console.WriteLine("您输入的不是数字,请重新输入、、");
                isnumber = false;
            }
            }
            for (int i = 0; i < number + 1; i++)
            {
                Console.WriteLine("{0}+{1}={2}",i,number-i,number);
            }
            Console.ReadKey();
由于用户输入的内容将会被作为字符串处理,需要将其转换成整型,当输入的不是数字时,将捕获异常,程序将跳过try{},直接执行catch{}中内容

评分

参与人数 1技术分 +1 收起 理由
李荣壮 + 1

查看全部评分

回复 使用道具 举报
刘波 黑马帝 2011-12-19 14:27:22
12#
try
{
//可能会发生异常的代码
}
catch(Exception e)
{
//这里你可以处理,比如弹出对话框告诉用户,或者把这个异常的情况(e)发邮件到你的邮箱,反正你想怎么处理就怎么处理。
//如果不处理就抛出,待下次调用这个方法时来处理throw ....
}
finally
{
//不管有没有异常都会执行这里的代码
}

希望对你有帮助
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马