user3192348
user3192348

Reputation: 9

Need to figure out how to use Try, Catch, and Finally in my code?

I am a very beginner programmer, if even that. I have to take a programming course as one of my classes in high school, so I am trying my best to make it through not really understanding much. With that said, please go easy on me.

For a project, I need to go back to two programs I created for a previous assignment, a program for determining an arithmetic sequence, and a program for calculating the volume of a cube or sphere.

The first instructions are to write the program so that it asks the user to define all variables. For example, the program would ask the user to input the value of the length of one side or the radius for the Cube and Sphere program.

I believe I already did this when I wrote the program, but I am struggling with the second set of directions, which states to "include the key words Try, Catch, and Finally, and have them function properly (ie. if the user defines an impossible calculation, there must be an error message shown; if not, there must be some action to indicate there was no error even if it is simply showing the answer)".

I really do not know how to put these statements into my code. If someone could explain or give me an example of them in my code I would very much appreciate it! Here is my code:

Volume of a Cube or Sphere

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

Arithmetic Sequence

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

Upvotes: 0

Views: 999

Answers (1)

NoChance
NoChance

Reputation: 5762

Computers could encounter a problem when performing an operation. For example if someone asks you to calculate the cube of x but enters x as "abc", the computer can't do that. Good programs validate data before performing calculations and try to provide a gentle handling of errors by warning the user with a friendly message amongst other possible actions. In you code you write:

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

This code could cause an error if you try a non-numeric value. A better way to write this is to use a method called TryParse, however, for the sake of your question we'll use Try/Catch. Try/Catch allows the program to try to perform one or more commands and if any of them causes an error the control flows to the catch part where the programmer is able to handle the error.

Applying this concept to your code, this is one way to use 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()

Try the above code when x is ABC. It is best to do that during debug so you can see the program flow. You should not have 1 big try/catch in your program. Instead, you generally place several such blocks only where you anticipate problems and where your code could prevent damage or provide useful feedback for the user.

Here is another example about this topic. MSDN-TryCatch1 MSDN-TryCatch2

Note that you should handle the case where the user could enter a negative value for X. This does not require a try/catch, rather an 'if'.

Upvotes: 3

Related Questions