尝试/捕获帮助,预期结束语句

时间:2011-09-18 01:15:34

标签: vb.net visual-studio-2010 try-catch

VB世界的新手,我正在做作业。我正在尝试设置嵌套的try / catch块,以确保来自3个文本框的值是有效的计算值。我被困了,因为我似乎无法弄清楚为什么我得到一个“预期结束语”错误。 try块的所有3行都在它们下面有一条波浪线。这是我的代码:

Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click

    Dim FutureValueDecimal, InvestmentAmountDecimal, InterestRateDecimal As Decimal
    Dim YearsInteger As Integer

    Try InvestmentAmountDecimal = Decimal.Parse(InvestmentAmountTextBox.text)

          Try InterestRateDecimal = Decimal.Parse(InterestRateTextBox.text)

            Try YearsInteger = Integer.Parse(yearsTextBox.text)
            Catch InvalidYears As FormatException
                MsgBox("Please enter a valid number of years", MsgBoxStyle.Exclamation, "Error")
                YearsTextBox.SelectAll()
            End Try

        Catch InvalidInterest As FormatException
            MsgBox("Please enter a valid interest rate.", MsgBoxStyle.Exclamation, "Error")
            InterestRateTextBox.SelectAll()
        End Try
    Catch InvalidAmount As FormatException
        MsgBox("Please enter a valid investment amount.", MsgBoxStyle.Exclamation, "Error")
    End Try

    FutureValueDecimal = InvestmentAmountDecimal * (1D + InterestRateDecimal) ^ YearsInteger

            FutureValueTextBox.Text = FutureValueDecimal.ToString("C")



End Sub

1 个答案:

答案 0 :(得分:5)

有人说每次尝试后你都要换行

所以改变这一行

   Try InterestRateDecimal = Decimal.Parse(InterestRateTextBox.text)

 Try
 InterestRateDecimal = Decimal.Parse(InterestRateTextBox.text)

同样适用于其他所有尝试。