简体   繁体   中英

vb.net if else logic flow error

I am debugging some code in an ASP.net web application project.

I have an If/Else statement nested in a Try/Catch block. Whenever an error occurs within the If block, rather than immediately jumping into the Catch block, it falls into the Else block.

I have stepped through the code repeatedly to witness this happening. When I throw an exception within the if block, I would expect logic to flow into the catch block, and I would never expect to see the if block get hit, and then the else block get hit, that seems to completely defeat the purpose of the if/else logic.

Here is the code in question:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    LoadDocument()
End Sub

Private Sub LoadDocument()
    Dim taDocuments As New SecureTableAdapters.IndividualDocumentsTableAdapter
    Dim dtDocuments As New Secure.IndividualDocumentsDataTable
    Dim iDocumentID As Integer = CInt(Request.QueryString("iDocumentID"))
    Dim sFileName As String
    Dim isMac As Boolean = clsApplication.isMac(Request.Browser.Platform)
    Dim bDownloaded As Boolean = False

    Try
        If taDocuments.FillBy_IndividualDocumentID(dtDocuments, iDocumentID) > 0 Then
            Dim oRow As Secure.IndividualDocumentsRow = dtDocuments.Rows(0)
            sFileName = "Statement-" & oRow.sFileNumber & ".pdf"

            If oRow.sDocumentName.ToUpper.Contains("LEDES") Or oRow.sDocumentName.ToUpper.Contains("TEXT") Then
                sFileName = sFileName.Replace("pdf", "txt")
            End If

            Dim b As Byte() = Nothing

            If oRow.IsbExtractedNull = False AndAlso oRow.bExtracted Then

                Dim sHost As String = "206.220.201.175"
                Dim sPath As String = String.Format("/Legacy/{0}/{1}/{2}.pdf", oRow.iFirmID.ToString, "Individuals", oRow.iIndividualDocumentID.ToString)
                b = DownloadDocument(sHost, sPath)

                If b Is Nothing Then
                    'When this line is hit, logic jumps to the else block with the comment below
                    Throw New Exception("FTP Download Failed")
                Else
                    bDownloaded = True
                End If
            Else
                bDownloaded = False
            End If

            If bDownloaded = False Then
                b = getImage(iDocumentID, "oPDF", "iIndividualDocumentID", "tblIndividualDocuments")
                If b Is Nothing Then
                    Throw New Exception
                End If
            End If

            If isMac Then
                Response.ContentType = "application/x-macbinary"
            Else
                Response.ContentType = "application/octet-stream"
            End If

            Response.Expires = 0
            Response.AddHeader("Content-Disposition", "attachment; filename=""" & sFileName & """")
            Response.BinaryWrite(b)
        Else
            '--->When the exception occurs, logic jumps to this else block
            Throw New Exception
        End If
    Catch ex As Exception
        Response.Write("Sorry, that statement could not be located. Please try again, or call us at xxx.xxx.xxxx for further information.")
        clsApplication.EmailError("An error occurred downloading a statement: " & vbCrLf & ex.Source & vbCrLf & ex.Message & vbCrLf & ex.StackTrace)

    End Try

End Sub

How is this possible?

It sounds like you're using the debugger with Optimizations still turned on. This can cause the Step Traceing to act in seemingly illogical and even impossible ways. This is caused because after the optimizer moves the code and variables around and consolidates different statements, there is no longer a simple or straight-forward relationship between the lines of source-code and the executable instructions.

Sounds like the code you're debugging against might be out of sync with the code that's executing. Try rebuilding the whole solution.

I think you are probably mistaken:

Try the following in simple vb .net winforms program.

Public Class Form1

   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      Try
         Dim doIt As Boolean = True
         If doIt Then
            Throw New Exception("throwing that stuff")
         Else
            MsgBox("in here - how did that happen?")

         End If
      Catch ex As Exception
         MsgBox(String.Format("That makes sense.  Exception is: {0}", ex))
      End Try


   End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM