无法捕捉到。 DEF'使用Try Catch

时间:2015-07-31 11:26:13

标签: vb.net try-catch

我正在使用vb.net(Visual Studio 2013)并且必须在快速循环(0.000001 s)中执行一些数学运算:我得到n. def(非数字)作为结果。我想找出错误发生的位置,以便能够解决它;我尝试使用Try...Catch但没有成功。我还能尝试什么?谢谢你的帮助

   ' ---------------------------------------------------------------------------------------------------
    '                                   Lateral Segment Area Case 4
    ' ---------------------------------------------------------------------------------------------------
    ''' <summary>
    ''' LateralSegmentArea4: lateral segment area type 4
    ''' </summary>
    ''' <param name="y">crcle radius</param>
    ''' <param name="xm">radius of screw circle</param>
    ''' <param name="na">number of screws</param>
    ''' <param name="Fa">area calculated</param>
    ''' <param name="Test">Test: true|false</param>
    ''' <param name="destination">logging path</param>
    ''' <returns>status</returns>
    ''' <remarks></remarks>
    Public Shared Function LateralSegmentArea4(y As Double,
                                              xm As Double,
                                              na As Double,
                                              ByRef Fa As Double,
                                              Test As Boolean,
                                              destination As String) As Boolean
    Dim ret As Boolean = True
    Dim status As Boolean = True
    Dim logstr As String = String.Empty
    Dim psi As Double = 0
    Dim ym As Double = 0
    Dim found As Boolean = False
    Dim alpha1 As Double = 0
    Dim alpha2 As Double = 0
    Dim phi As Double = 0
    Dim x1 As Double = 0
    Dim y1 As Double = 0
    Dim x2 As Double = 0
    Dim y2 As Double = 0
    Dim Aseg As Double = 0
    Dim result As Double = 0

    Try
        ' psi
        psi = 360 / na

        ' crosspoints x1, x2
        ret = General.CrosspointsCircleStraightline(xm, ym, y, psi / 2, x2, x1, found, destination)
        If found = True Then
            y1 = Math.Tan(psi / 2 * Math.PI / 180) * x1
            y2 = Math.Tan(psi / 2 * Math.PI / 180) * x2

            ' phi
            alpha1 = Math.Asin(y1 / y)
            If x2 >= xm Then
                alpha2 = Math.Asin(y2 / y)
            Else
                alpha2 = Math.PI - Math.Asin(y2 / y)
            End If
            phi = Math.PI - alpha1 - alpha2

            ' segment area
            Aseg = Math.Pow(y, 2) / 2 * (phi - Math.Sin(phi))

            ' lateral area
            result = 2 * Aseg
            Fa = result

            If Test Then
                Logging.WriteLogFile("", False, destination)
                Logging.WriteLogFile("LateralSegmentArea4", False, destination)
                Logging.WriteLogFile("x1: " & x1, False, destination)
                Logging.WriteLogFile("y1: " & y1, False, destination)
                Logging.WriteLogFile("x2: " & x2, False, destination)
                Logging.WriteLogFile("alpha1: " & alpha1, False, destination)
                Logging.WriteLogFile("alpha2: " & alpha2, False, destination)
                Logging.WriteLogFile("phi: " & phi, False, destination)
                Logging.WriteLogFile("Aseg: " & Aseg, False, destination)
            End If
        End If
    Catch ex As Exception
        logstr = "Error while getting lateral segment area 4 (" & ex.ToString & ")"
        Logging.WriteLogFile(logstr, True, destination)
        status = False
    End Try

    If status = True Then status = ret
    Return status
End Function

' ---------------------------------------------------------------------------------------------------
'                                   Crosspoints Circle Straight line
' ---------------------------------------------------------------------------------------------------
''' <summary>
''' CrosspointsCircleStraightline: determines crosspoint of circle and straight line
''' </summary>
''' <param name="xm">x-position of srew circle</param>
''' <param name="ym">y-position of srew circle</param>
''' <param name="r">circle radius</param>
''' <param name="phi">angle of straight line</param>
''' <param name="x1">x1-crosspoint</param>
''' <param name="x2">x2-crosspoint</param>
''' <param name="found">crosspoint found: true | false</param>
''' <param name="destination">logging path</param>
''' <returns>status</returns>
''' <remarks></remarks>
Public Shared Function CrosspointsCircleStraightline(xm As Double,
                                                             ym As Double,
                                                             r As Double,
                                                             phi As Double,
                                                             ByRef x1 As Double,
                                                             ByRef x2 As Double,
                                                             ByRef found As Boolean,
                                                             destination As String) As Boolean

    Dim ret As Boolean = True
    Dim status As Boolean = True
    Dim logstr As String = String.Empty
    Dim a As Double = 0
    Dim part0 As Double = 0
    Dim part1 As Double = 0
    Dim part2 As Double = 0
    Dim part3 As Double = 0
    Dim part4 As Double = 0
    Dim angle As Double = 0

    Try
        found = False
        angle = phi * Math.PI / 180
        a = Math.Tan(angle)

        ' part0 = 2*xm + 2*a*ym
        part0 = 2 * xm + 2 * a * ym

        ' part1 = (2*xm + 2*a*ym)^2
        part1 = Math.Pow((2 * xm + 2 * a * ym), 2)

        ' part2 = 4*(1+a^2)*(xm^2+ym^2-r^2)
        part2 = 4 * (1 + Math.Pow(a, 2)) * (Math.Pow(xm, 2) + Math.Pow(ym, 2) - Math.Pow(r, 2))

        ' part3 = 2*(1+a^2)
        part3 = 2 * (1 + Math.Pow(a, 2))

        'part4 = (2*xm + 2*a*ym)^2 - 4*(1+a^2)*(xm^2+ym^2-r^2)
        part4 = part1 - part2
        If part4 < 0 Then
            Threading.Thread.Sleep(100)
            ' repeat
            part4 = part1 - part2
        End If

        ' crosspoint found?
        If part4 > 0 Then
            found = True
            x1 = (part0 + Math.Sqrt(part4)) / part3
            x2 = (part0 - Math.Sqrt(part4)) / part3
        Else
            found = False
        End If
    Catch ex As Exception
        logstr = "Error while finding cross points (" & ex.ToString & ")"
        Logging.WriteLogFile(logstr, True, destination)
        status = False
    End Try

    If status = True Then status = ret
    Return status
End Function

0 个答案:

没有答案