Solutions for those exercises.

Questions

  1. In computing, what is an exception?

    • A compilation error.
    • When a program user requires a special accommodation.
    • When a behaviour not supposed to happen occurs during execution.
    • A keyword.
  2. When a program meets an unexpected behaviour, we say that it…

    • raises an exception.
    • throws an exception.
    • All of the above.
  3. An exception can occur when…

    • … a user enters for example the string “Test” when asked for a numerical value.
    • … a division by 0 occurs.
    • … the program tries to access an array outside of its index range.
    • All of the above.
  4. A try-catch block…

    • … executes all the code inside its try block, then all its code inside its catch block.
    • … executes all the code inside its try block, then all its code inside its catch block if an exception was raised at any point.
    • … executes only if an exception was raised in the program before.
    • … executes the code inside its try block, and switches to its catch block if an exception was thrown.
    • … executes its catch block first, and then its try block if an exception was raised.
  5. A try-catch-finally block…

    • … can have multiple catch block.
    • … can omit the finally block.
    • … can omit the catch block.
    • All of the above.

Warm-up Exercise

  1. Consider the following code:

    using System;
     
    class Program
    {
      static void Main()
      {
        try
        {
          Console.WriteLine("Enter a number");
          int uInput = int.Parse(Console.ReadLine());
          int[] t = { 10 };
          int div = 0 / (uInput - 1);
          int tAcces = t[uInput];
        }
        catch (IndexOutOfRangeException)
        {
          Console.WriteLine("IndexOutOfRangeException");
        }
        catch (DivideByZeroException)
        {
          Console.WriteLine("DivideByZeroException");
        }
        catch (FormatException)
        {
          Console.WriteLine("FormatException");
        }
        catch (ArgumentNullException)
        {
          Console.WriteLine("ArgumentNullException");
        }
      }
    }

    (Download this code)

    • Determine which input would the user needs to enter to get “IndexOutOfRangeException”, “DivideByZeroException”, “FormatException” and “ArgumentNullException” displayed.
    • Is there something the user could enter that would not raise any exception?

Problems

  1. We implement two methods for a simple Calculator static class and use one of them.

    1. Write a Square method that takes one int argument and returns its value squared if the parameter is less than 463411, and throws an OverflowException exception otherwise.
    2. Write a Divide method that takes two int arguments and returns the result of dividing the first parameter by the second. If the second parameter is 0, then the method should throw an ArgumentOutOfRangeException exception.
    3. Write a piece of code (to be inserted into a Main method) that asks the user to enter one number. Then, call Square with it (no need to loop). Your code should not check the value of the string entered for the operation, but must catch the exceptions potentially thrown by the Square method (and, as a bonus, by the Parse method). Your code should also display “Thanks!” regardless of whether an exception was thrown.

Footnotes

  1. The square of is greater than the maximum value an int can hold, int.MaxValue.