C# Tutorial - Application 4 – Catching the Exceptions


Create a new console Application called App04. We are going to create another application to add two whole numbers together and output the result, but this time we will include some error handling. A full listing for the application is given below:

static void Main(string[] args)
{
  // Storage
  int value1 = 0;
  int value2 = 0;
  int total = 0;

  // Get first number
  first:
  try
  {
    Console.Write("Enter a whole number: ");
    value1 = System.Convert.ToInt32(Console.ReadLine());
  }
  catch (System.ArgumentNullException)
  {
    Console.WriteLine("String is null.");
    goto first;
  }
  catch (System.FormatException)
  {
    Console.WriteLine("String does not consist of an " +
        "optional sign followed by a series of numbers.");
    goto first;
  }
  catch (System.OverflowException)
  {
    Console.WriteLine("Overflow in string to int conversion.");
    goto first;
  }

  // Get second number, same as first number except for value2
  second:
  try
  {
    Console.Write("Enter a whole number: ");
    value2 = System.Convert.ToInt32(Console.ReadLine());
  }
  catch (System.ArgumentNullException)
  {
    Console.WriteLine("String is null.");
    goto second;
  }
  catch (System.FormatException)
  {
    Console.WriteLine("String does not consist of an " +
        "optional sign followed by a series of numbers.");
    goto second;
  }
  catch (System.OverflowException)
  {
    Console.WriteLine("Overflow in string to int conversion.");
    goto second;
  }

  // Add them together
  total = value1 + value2;

  // Output the answer
  Console.WriteLine("The total is {0}", total);
}

This application introduces two new concepts with the main one being exception handling. In the code above, each incorrectly entered first number is handled, and control is passed back to the point labelled first: using the goto statement. The second number uses the point labelled second:.

The goto Statement

The goto statement transfers the program control directly to a labelled statement and can take one of the following forms:

goto identifier;

The identifier indicates a label located in the current scope.

goto case identifier;

The identifier indicates a specific switch-case label in a switch statement to transfer control to (switch statements will be covered later).

goto default;

Control is transferred to the default case in a switch statement (switch statements will be covered later).

Nested Loops

The goto statement is also useful to get out of deeply nested loops. In the following example, when i is equal to 5 and j is equal to 5, then control jumps out of both loops to the label Reached5.

using System;

public class GotoTest
{
  static void Main()
  {
    for(int i = 0; i < 10; i++)
    {
      for(int j = 0; j < 10; j++)
      {
        if( (i == 5) && (j == 5) )
        {
          goto Reached5;
        }
      }
    }

    Reached5:
    Console.WriteLine("i = {0}, j = {1}", i, j);
  }
}

goto has the potential to create spaghetti code which will be complex and painful to maintain, so use it sparingly.


<< Previous Contents Next >>

© Publicjoe, 2008