改进异常处理vb.net [英] improve the exception handling vb.net

查看:122
本文介绍了改进异常处理vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理异常时,我遇到了一些小问题。
我正在建立... aaaaa ... soooo ...嗯...一个图形数据库?是啊这样的东西...用vb.net。
我正在构建解析器,用于处理db(?)的基本功能会发生什么?用户进行研究,程序选中它们,并允许进行一些计算。通常字符串处理,但是为了给出一个完整的工具,我还实现了数学函数。

没有办法知道他将要插入的开发时间数学的价值。特别是,没有办法知道出现遍历的列的每一行是否具有相同的数据类型。它是一个无模式的数据库(?)。
那么发生什么...我必须实现错误处理。

 对于i As Integer = 0 To pR .r.wsRowCount  -  1 
尝试
pR.r.wsItem(cix,i)= Convert.ToDouble(pR.r.wsItem(ca.ix,i))* Convert.ToDouble(pR。 r.wsItem(cb.ix,i))
Catch
pR.r.wsItem(cix,i)= String.Empty
结束尝试


下一个

这样的东西...
(psss .. Convert.toDouble() cDbl() Double.Parse()等)。
但是当我运行它时,如果所有行(基准约2000行),真的是数字...几毫秒,但如果一些行不是数字,它需要3或4秒。最糟糕的情况是,如果用户应该在字符串列中进行数学运算...再见,同时最好去喝咖啡。
所以我的电脑可能不是世界上最先进的,但是您是否知道有没有办法避免错误处理中的这种难以置信的延迟?我记得这是一个查询的解析器,我会避免使它很重。

解决方案

你正在使用例外当您的代码遇到无法转换为双精度的值时修复问题。这被称为使用异常驱动代码,这是一个不好的做法。捕捉异常从来不是这种情况的一个很好的解决方案。



特别是当有一个明确的方法来避免他们

 对于i As Integer = 0到pR.r.wsRowCount  -  1 
Dim d1 As Double
Dim d2 As Double

如果Double.TryParse(pR.r.wsItem(ca .ix,i),d1)然后
如果Double.TryParse(pR.r.wsItem(cb.ix,i))d2然后
pR.r.wsItem(cix,i)=(d1 * d2).ToString()
else
pR.r.wsItem(cix,i)= String.Empty
End If
Else
pR.r.wsItem (cix,i)= String.Empty
结束如果
下一个

Convert.ToDouble 不能处理字符串不是有效数值的事实,如果遇到这种情况,它会引发异常。抛出异常在性能方面是昂贵的用于收集关于调用堆栈的信息。



相反,如果您的输入包含无法转换为double的值,然后 Double.TryParse 是正确的方法,如果您的基准在性能上有一点差异。 Double.TryParse 如果传递的字符串无法转换为double,但返回false,则不引发异常。以这种方式避免了昂贵的例外,并且您有更可预测的执行时间



说,您应该重新评估您的方法来存储某些字符串中的每种数据结构体。在字符串和预期数据类型之间进行不断转换的需要可能是真正的瓶颈。


I have a small trouble with the performance of the exception handling. I'm building... aaaaa... soooo...hmmm...a graph db? yeah something like this... with vb.net. I'm building the parser, for handling the basic functions of the db(?) What happen? The user makes a research, the program tab them, and allow to make some computation . Normally string handling, but, to give a complete instrument, I implement also mathematical functions.
There is no way to know developing-time math kind of value he is going to insert. And especially, there is no way to know if each row of the column that comes out the traversal has the same data type. It is a schema-free db(?). So what happen...I have to implement the error handling.

   For i As Integer = 0 To pR.r.wsRowCount - 1
                    Try
                        pR.r.wsItem(cix, i) = Convert.ToDouble(pR.r.wsItem(ca.ix, i)) * Convert.ToDouble(pR.r.wsItem(cb.ix, i))
                    Catch
                        pR.r.wsItem(cix, i) = String.Empty
                    End Try


                Next

Something like this... (psss.. Convert.toDouble() is the most performant function between cDbl(), Double.Parse() etc). But when I run it, if all the rows (benchmark around 2000 rows), are really numeric... few milliseconds, but if some rows are not numeric, it require 3 or 4 seconds. And worst scenario, if the user should do a mathematical operation in a string column... goodbye, meanwhile is better go to take a coffee. So my computer may be is not the most advanced in the world, but are you aware if there is any way to avoid this incredible delay in the error handling? I would remember that this is the parser of a query and I would avoid to make it heavy.

解决方案

You are using Exceptions to fix a problem when your code encounter a value that cannot be converted to a double. This is called driving your code using exceptions and it is a bad practice. Catching exceptions is never a good fix for this scenario.

In particular when there is a clear way to avoid them

 For i As Integer = 0 To pR.r.wsRowCount - 1
     Dim d1 As Double
     Dim d2 As Double

     if Double.TryParse(pR.r.wsItem(ca.ix, i), d1) Then
        if Double.TryParse(pR.r.wsItem(cb.ix, i), d2 Then
             pR.r.wsItem(cix, i) = (d1 * d2).ToString()
        else
             pR.r.wsItem(cix, i) = String.Empty
        End If
     Else
        pR.r.wsItem(cix, i) = String.Empty
     End If
 Next

Convert.ToDouble cannot handle the fact that a string is not a valid numeric value and if it encounter this case it throws an exception. Throwing an exception is costly in terms of perfomance in particular for gathering the information about the call stack.

Instead, if there is the possibility that your input contains values that cannot be converted to double, then Double.TryParse is the correct way to proceed also if your benchmarks show a little difference in performance. Double.TryParse doesn't raise an exception if the string passed cannot be converted to a double, but return false. In this way the costly exception is avoided and you have a more predictable time of execution

Said that, you should really reevaluate your approach to store every kind of data in some string structure. The need of constant conversion between a string and the intended data type could be a real bottleneck here.

这篇关于改进异常处理vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆