Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

vb.net - How do I use Try, Catch, and Finally

My assignment is to write a program that asks the user for 10 numbers which it will then find the average of, and must include Try, Catch, and Finally keywords. (Divide by zero exception).

How to I use Try, Catch, and Finally?

So far my program looks like this:

Module Module1
    Public Sub Main()
            Dim A, B, C, D, E, F, G, H, I, J, K, L, M As Integer
            Console.WriteLine("Enter 1st Number: ")
            A = Console.ReadLine()
            Console.WriteLine("Enter 2nd Number: ")
            B = Console.ReadLine()
            Console.WriteLine("Enter 3rd Number: ")
            C = Console.ReadLine()
            Console.WriteLine("Enter 4th Number: ")
            D = Console.ReadLine()
            Console.WriteLine("Enter 5th Number: ")
            E = Console.ReadLine()
            Console.WriteLine("Enter 6th Number: ")
            F = Console.ReadLine()
            Console.WriteLine("Enter 7th Number: ")
            G = Console.ReadLine()
            Console.WriteLine("Enter 8th Number: ")
            H = Console.ReadLine()
            Console.WriteLine("Enter 9th Number: ")
            I = Console.ReadLine()
            Console.WriteLine("Enter 10th Number: ")
            J = Console.ReadLine()
            K = (A+B+C+D+E+F+G+H+I+J)
            Console.WriteLine("Enter the amount of numbers to average: ")
            M = Console.ReadLine()
            L = K / M
            Console.WriteLine("The Average Is: " & L)
            Console.ReadKey()
    End Sub
End Module
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A Try, Catch, Finally block is extremely useful for handling errors where in a normal instance it would cause the program to crash.

For example:

Dim n As Integer
Dim a As Integer = 0
Dim b As Integer = 1
Try
    n = b / a
Catch
    MsgBox("We've crashed :(")
Finally
    MsgBox("..but we're still alive!")
End Try

You are also able to get the information on the exact error, a possible use of this is that you might want to filter it out so specific errors are ignored, like per se:

Dim n As Integer
Dim a As Integer = 0
Dim b As Integer = 1
Try
    n = a / b
Catch ex As DivideByZeroException
    MsgBox("We've crashed, here's the specific error: " + ex.Message)
Catch ex As Exception
    MsgBox("Some other error happened: " + ex.Message)
Finally
    MsgBox("..but we're still alive!")
End Try

The three parts:

  1. Try: Try executing the code within this block, if it fails;

  2. Catch: Catch the exception/error and execute the code inside this block

  3. Finally: Finally execute code inside this block regardless of what happened in the Try and Catch components.

For example you could use something like this for your specific use case:

[...]
Try
    L = K / M
    Console.WriteLine("The Average Is: " & L)
    Console.ReadKey()
Catch
    Console.WriteLine("Uh oh, we've divided by 0!")
Finally
    Console.WriteLine("Press any key to continue.")
    [...]
End Try

The official documentation has some handy information.

As a user who commented on your question said (Mark), there's other issues with your code (not covering them because it'll go outside the scope of the question) and you should turn on Option Strict to see them. Your code could also be made more efficient by utilising a For loop and an Array or List, but I'll leave that to you to do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...