【问题标题】:Need to figure out how to use Try, Catch, and Finally in my code?需要弄清楚如何在我的代码中使用 Try、Catch 和 finally?
【发布时间】:2014-02-01 23:39:35
【问题描述】:

即使如此,我也是一个非常初学者的程序员。我必须在高中时将编程课程作为我的课程之一,所以我正在尽最大努力通过对它的了解不多。话虽如此,请对我放轻松。

对于一个项目,我需要回到我为之前的作业创建的两个程序,一个用于确定等差数列的程序,以及一个用于计算立方体或球体体积的程序。

第一条指令是编写程序,要求用户定义所有变量。例如,程序会要求用户输入立方体和球体程序的一侧的长度或半径值。

我相信我在编写程序时已经这样做了,但是我正在努力解决第二组指示,其中指出“包括关键字 Try、Catch 和 Final,并让它们正常运行(即,如果用户定义了一个不可能的计算,必须显示错误消息;如果没有,即使只是显示答案,也必须有一些动作表明没有错误)"。

我真的不知道如何将这些语句放入我的代码中。如果有人可以在我的代码中解释或给我一个例子,我将非常感激!这是我的代码:

立方体或球体的体积

Sub Main()
    Dim Type As String
    Dim Size As String
    Dim Length_Radius As Double
    Dim Output As Double

    Dim Value As Double
    Console.WriteLine("Would you like to calculate the volume of a (C)ube or a (S)phere? *press either C or S then enter to continue*")
    Type = Console.ReadLine
    Type = Convert.ToString(Type)
    Value = Asc(Type)
    If (Value = 67) Or (Value = 99) Then
        Console.WriteLine("The equation is x^3 where x is the length of a side. What would you like the value of x to be?")
        Size = Console.ReadLine
        Length_Radius = Convert.ToDouble(Size)
        Output = (Length_Radius * Length_Radius * Length_Radius)
        Console.WriteLine("The value of the volume of the Cube is: " & Output & ".")
    ElseIf (Value = 83) Or (Value = 115) Then
        Console.WriteLine("The equation is 4/3*pi*r^3 where r is the radius. What would you like the value of the radius to be?")
        Size = Console.ReadLine
        Length_Radius = Convert.ToDouble(Size)
        Output = ((4 / 3) * 3.14 * (Length_Radius * Length_Radius * Length_Radius))
        Console.WriteLine("The value of the volume of a sphere is: " & Output & ".")
    ElseIf (Value <> 83) Or (Value <> 115) Or (Value <> 67) Or (Value <> 99) Then
        Console.WriteLine("You have inputted an incorrect value.")

    End If

    Console.ReadLine()



End Sub

算术序列

Sub Main()
    Dim Letters As String
    Dim a_n, d, a_one As Double
    Dim Output As Double
    Console.WriteLine("This program will perform the arithmetic sequence (a(n) = a(1) + d(a(n) - 1)")
    Console.WriteLine("What would you like the value of 'a(n)' to be? *The total number of times the sequence will repeat.*")
    Letters = Console.ReadLine
    a_n = Convert.ToDouble(Letters)
    Console.WriteLine("What would you like the value of 'a(1)' to be? *The starting value.*")
    Letters = Console.ReadLine
    a_one = Convert.ToDouble(Letters)
    Console.WriteLine("What would you like the value of 'd' to be? *The number that the equation is multiplied by*")
    Letters = Console.ReadLine
    d = Convert.ToDouble(Letters)
    Output = (a_one + d * (a_n - 1))
    Console.WriteLine("The value of the arithmetic sequence is: " & Output & ".")
    Console.ReadLine()
End Sub

【问题讨论】:

  • 您是否阅读过有关 try, catch, finally 的文档?
  • 如果通过文档您的意思是实际上学习了 Try、Catch 和 finally 是什么,那么是的,我确实在给定的课程中学到了一些关于它们的知识,但我似乎无法理解编程。
  • 您是否尝试过文档中的一些示例?

标签: vb.net visual-studio exception try-catch finally


【解决方案1】:

计算机在执行操作时可能会遇到问题。例如,如果有人要求您计算 x 的立方,但输入 x 为“abc”,则计算机无法执行此操作。好的程序在执行计算之前验证数据,并尝试通过在其他可能的操作中以友好的消息警告用户来温和地处理错误。在您编写的代码中:

        Size = Console.ReadLine
        Length_Radius = Convert.ToDouble(Size)

如果您尝试使用非数字值,此代码可能会导致错误。编写此代码的更好方法是使用称为 TryParse 的方法,但是,为了您的问题,我们将使用 Try/Catch。 Try/Catch 允许程序 try 执行一个或多个命令,如果其中任何一个导致错误,则控制流向程序员能够处理的 catch 部分错误。

将此概念应用于您的代码,这是使用 Try/Catch 的一种方式:

    Dim Type As String
            Dim Size As String
            Dim Length_Radius As Double
            Dim Output As Double

            Dim Value As Double
            Console.WriteLine("Would you like to calculate the volume of a (C)ube or a (S)phere? *press either C or S then enter to continue*")
            Type = Console.ReadLine
            Type = Convert.ToString(Type)
            Value = Asc(Type)

            Try


            If (Value = 67) Or (Value = 99) Then
                Console.WriteLine("The equation is x^3 where x is the length of a side. What would you like the value of x to be?")
                Size = Console.ReadLine
                Length_Radius = Convert.ToDouble(Size)
                Output = (Length_Radius * Length_Radius * Length_Radius)
                Console.WriteLine("The value of the volume of the Cube is: " & Output & ".")
            ElseIf (Value = 83) Or (Value = 115) Then
                Console.WriteLine("The equation is 4/3*pi*r^3 where r is the radius. What would you like the value of the radius to be?")
                Size = Console.ReadLine
                Length_Radius = Convert.ToDouble(Size)
                Output = ((4 / 3) * 3.14 * (Length_Radius * Length_Radius * Length_Radius))
                Console.WriteLine("The value of the volume of a sphere is: " & Output & ".")
            ElseIf (Value <> 83) Or (Value <> 115) Or (Value <> 67) Or (Value <> 99) Then
                Console.WriteLine("You have inputted an incorrect value.")

            End If


            Catch ex As Exception
                'VB.NET will capture the error text in a property called ex.Message. 
                'Let's show this message to the user as follows:
                Console.WriteLine(" A problem occurred:" + Environment.NewLine + ex.Message)

            Finally
               'Logic will come here eventually after calculation is attempted
                Console.WriteLine("I am done with calculations")

            End Try


            Console.WriteLine("Press enter to exit")
            Console.ReadLine()

当 x 是 ABC 时,试试上面的代码。最好在调试期间这样做,这样您就可以看到程序流程。你的程序中不应该有 1 次大的 try/catch。相反,您通常只在您预计会出现问题并且您的代码可以防止损坏或为用户提供有用反馈的地方放置几个这样的块。

这是有关此主题的另一个示例。 MSDN-TryCatch1 MSDN-TryCatch2

请注意,您应该处理用户可以为 X 输入负值的情况。这不需要 try/catch,而是需要 'if'。

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2013-11-21
    • 2014-11-27
    • 1970-01-01
    • 2022-07-01
    • 2015-03-16
    • 1970-01-01
    • 2016-12-25
    相关资源
    最近更新 更多