c#编程最佳实践1:异常处理

你是否在用户输入验证中使用异常处理机制?

如果是,那么你就是那个把你的项目执行速度降低了62倍的人。你不相信我吗?等几分钟;我来教你怎么做。但是在这个例子之前,让我们了解一下在什么地方需要异常处理。

例如,你正在验证用户的数据,对于任何无效的输入,你将引发一个异常并将其抛出给客户端,如下所示:

  •  
class BusinessLogcCheck  {      public void Check()      {          try          {              //Your validation code is here          }          catch (Exception ex)          {              throw new Exception("My own exception");          }      }  }

 亲爱的朋友,在下一个示例中,如果你看到输出屏幕,你将意识到这种做法有多糟糕。让我们看看下面的代码。

  •  
  •  
using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Diagnostics;  using System.IO;  using System.Net;  using System.Net.NetworkInformation;  namespace Test1  {      class Program      {          public static void ThrowTest()          {              throw new Exception("This is exceptopn");          }          public static Boolean Return()          {              return false;          }          static void Main(string[] args)          {              Stopwatch sw = new Stopwatch();              sw.Start();              try              {                      ThrowTest();              }              catch              {              }              sw.Stop();              Console.WriteLine("With Exception " + sw.ElapsedTicks);              sw.Restart();              try              {                  Return();              }              catch              {              }              sw.Stop();              Console.WriteLine("With Return " + sw.ElapsedTicks);              Console.ReadLine();          }      }  }

这就是你等待的输出。

图片

我的概念证明非常简单。在一个函数中,我抛出了一个异常,在另一个函数中,我在检查用户输入后返回一个布尔值。我还附上了一个计算器的屏幕,让你相信异常处理是如何影响代码性能的。

因此,我们可以得出这样一个结论:“不要为用户输入验证引发异常。”使用布尔返回技术(或类似的技术)来验证业务逻辑中的输入”。因为异常对象的开销非常大。

02

永远不要在循环中实现try-Catch

是的,它也与异常处理有关。我重复“永远不要在循环中执行try-catch”。让我用一个例子来证明。 

  •  
  •  
using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Diagnostics;  using System.IO;  using System.Net;  using System.Net.NetworkInformation;  namespace Test1  {      class Program      {          static void Method1()          {              for (int i = 0; i < 1000; i++)              {                  try                  {                      int value = i * 100;                      if (value == -1)                      {                          throw new Exception();                      }                  }                  catch                  {                  }              }          }          static void Method2()          {              try              {                  for (int i = 0; i < 1000; i++)                  {                      int value = i * 100;                      if (value == -1)                      {                          throw new Exception();                      }                  }              }              catch              {              }          }          static void Main(string[] args)          {              Stopwatch sw = new Stopwatch();              sw.Start();              Method1();              sw.Stop();              Console.WriteLine("Within Loop " + sw.ElapsedTicks);              sw.Restart();              Method2();              sw.Stop();              Console.WriteLine("Outside of Loop " + sw.ElapsedTicks);              Console.ReadLine();          }      }  }

这是输出屏幕。

图片

在method1的这个程序中,我在for循环中实现了异常处理机制,而在method2中,我在没有循环的情况下实现了异常处理机制。我们的输出窗口表明,如果我们在for循环外实现try-catch程序的执行速度将比循环内的try-catch快2倍。

同样,唯一的结论是“不要在项目的循环中实现try-catch。(是的!不仅在for循环中,而且在任何循环中。)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值