-2

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
Kylie
  • 51
  • 3
  • 6
    [Here some the documentation for Try/Catch/Finally](http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx). It has an explanation of what they do and examples of how to use them. – Sam I am says Reinstate Monica Jan 09 '15 at 22:58
  • You have no need for a `finally`, because you're not allocating any resources that need to be disposed. You need a `try..catch` - `try` the division, and add the `catch` to handle the divide by zero error. (Or, more simply, check to make sure `M` is not zero before attempting the division in the first place.) – Ken White Jan 09 '15 at 23:12
  • 1
    @KenWhite The OP specifically said that they *needed* to use `Finally` for their assignment. I'm writing up an answer now so hopefully it's not closed during this time.. – AStopher Jan 09 '15 at 23:14
  • @cybermonkey: Yes, I covered that first in my comment. – Ken White Jan 09 '15 at 23:15
  • 2
    You should turn [`Option Strict On`](http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx) to see some other issues with your code, but by then you will really be struggling to find a reason for Try/Catch. :-) – Mark Jan 09 '15 at 23:18
  • @KenWhite - What does `Finally` have to do with disposal of resources? Finally is just a block of code that will always run, regardless of whether or not there is an exception. It need not have anything to do with disposal of resources. – Chris Dunaway Jan 12 '15 at 15:21
  • @Chris: In the code written, there is no need for a finally block for anything (with or without a try..catch). *Typically* (note I didn't say *always*) a finally block is used to ensure resources are released such as memory allocations, file handles, etc. – Ken White Jan 12 '15 at 15:31

1 Answers1

2

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.

Community
  • 1
  • 1
AStopher
  • 4,207
  • 11
  • 50
  • 75