C#第六回:try catch

 try 代码段中是用来捕获异常的操作,catch代码段中是捕获到异常以后进行的操作。

下面这段代码,当执行第七行的时候,会出错,然后会直接跳入catch中,然后继续执行catch之后的代码,

注意,try代码段中,第7行之后的所有代码都不会执行了。本实例中,指的是7和8行。显然,该标高还是被创建了,只是名称未修改。

 1.    using (Transaction ts=new Transaction(doc,"生成标高"))
 2.    {
 3.        ts.Start();
 4.        try
 5.        {
 6.            Level newlevel=Level.Create(doc,level.Elevation);
 7.            newlevel.Name=level.Name;//名称与现有层高重复的话,会报异常
 8.            newLevels.Add(newlevel);
 9.        }
10.        catch
11.        {
12.        }
13.        ts.Commit();
14.    }

 

下面这段代码,当执行第七行的时候,会出错,然后会直接跳入catch中,然后继续执行catch之后的代码,

注意,try代码段中,第7行之后的所有代码都不会执行了。本实例中,指的是7,8,9行。显然,该标高没有被创建了,事务的Commit方法并未执行。

 1.    try
 2.    {
 3.       using (Transaction ts=new Transaction(doc,"生成标高"))
 4.       {
 5.           ts.Start();
 6.           Level newlevel=Level.Create(doc,level.Elevation);
 7.           newlevel.Name=level.Name;
 8.           newLevels.Add(newlevel);
 9.           ts.Commit();
10.       }
11.    }
12.    catch
13.    {
14.    }

在catch中,有个Exception类型的变量ex,

在网上搜到的案例中都是输出ex.Message,输出异常错误,类似于没有引用对象之类的,其实价值不是特别高,个人觉得更有价值的是ex.StackTrace,可以明确输出具体是哪一个cs文件的哪一个方法出现了错误。看下图:

try catch的具体案例:

先上一段代码,

class Program
    {
        static void Main(string[] args)
        {
            ExceptionDemo demo = new ExceptionDemo();
            try
            {
                int result = demo.ParentMehtod(4);
                Console.WriteLine($"The number of the specified position is {result}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }
    }
class ExceptionDemo
    {
        public int ParentMehtod(int position)
        {
            int output = 0;
            Console.WriteLine("Open database connection");
            try
            {
                output = GetNumber(position);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                Console.WriteLine("Close database connection");
            }
            return output;
        }

        public int GetNumber(int position)
        {
            int[] numbers = new int[] { 1, 5, 7, 2 };
            return numbers[position];
        }
    }

现在我们假设在Program类中如果没有trycatch,只在ExceptionDemo中有trycatch,那么运行结果还是直接跳异常界面,因为在ExceptionDemo中,捕获到异常后的处理是throw,即抛出异常;

如果Program类中如果有trycatch,就像上面那段代码,我们再运行,就不会跳异常界面了,因为在ExceptionDemo中,虽然异常被抛出,但是在Program类中还有一个trycatch,就捕获到这个被抛出的异常,最后的处理方式是2个console.writeline输出异常信息。

 

注意了,在throw后面,尽量别跟ex,即throw ex.因为这样会隐藏掉一些stacktrace,

对比上面2张图,少了一行信息的就是throw ex的结果,多一行的是只有throw的结果。

throw这里也可以根据实际需要new一个新的exception

比如:

throw new ArgumentException("You give us a wrong position");

这样就把异常的类型给确定下来。下图就是VS界面中的该异常:

另外,可以有多个catch一起使用,这样就可以针对不同的异常,做出不同的处理

 catch(ArgumentException ex)
            {
                Console.WriteLine("You give us a wrong position");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值