关于c#:先使用Try Catch或校验值 | 珊瑚贝

Using Try Catch or check value first


我需要知道什么更好(对于性能和其他方面),在预期错误时使用 Try Catch 并且在错误发生时没有替代方案或首先检查值?

例如(VB.net):
如果我需要从数据库(例如来自数据读取器)中按值填充文本框,并且我期望一些空值,如果值为空,我会将文本框留空。

现在我有办法做到这一点:

1
2
3
4
try
  textbox1.text = DR(“Name”)
catch
end try

现在如果列名称包含空值,则会引发错误并且 textbox1 仍然为空。

1
If Not IsDBNull(DR(“Name”)) Then textbox1.Text = DR(“Name”)

什么更好?

非常感谢。

  • 您绝对应该尽可能避免任何尝试/捕获。 Try 几乎是免费的,但是当抛出异常时,比 catch 非常昂贵(谈论性能)


不要将异常处理用作流控制。永远。

所以 – 检查值而不是 Try/Catch.

当抛出异常时,运行时有很多工作要做 – 如果您期望 null 值 – 总是检查它们而不是依赖异常。


来源:https://www.codenong.com/13222796/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9242.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?