Questions
-
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.
-
When a program meets an unexpected behaviour, we say that it…
- … raises an exception.
- … throws an exception.
- All of the above.
-
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.
- … a user enters for example the
-
A
try-catchblock…- … executes all the code inside its
tryblock, then all its code inside itscatchblock. - … executes all the code inside its
tryblock, then all its code inside itscatchblock if an exception was raised at any point. - … executes only if an exception was raised in the program before.
- … executes the code inside its
tryblock, and switches to itscatchblock if an exception was thrown. - … executes its
catchblock first, and then itstryblock if an exception was raised.
- … executes all the code inside its
-
A
try-catch-finallyblock…- … can have multiple
catchblock. - … can omit the
finallyblock. - … can omit the
catchblock. - All of the above.
- … can have multiple
Warm-up Exercise
-
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"); } } }- 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?
Solution
| Exception | Input |
|---|---|
| ”IndexOutOfRangeException” | Any number greater than 2. |
| ”DivideByZeroException” | 1 |
| ”FormatException” | Any string that is not a number (for example, “Test”) |
| “ArgumentNullException” | A null string (ctrl + d on linux, ctrl + z on windows) |
Entering 0 would not raise any exception.
Problems
-
We implement two methods for a simple
Calculatorstaticclass and use one of them.-
Write a
Squaremethod that takes oneintargument and returns its value squared if the parameter is less than 463411, and throws anOverflowExceptionexception otherwise.Solution
public static int Square(int p){ if (p > 46341 || p < -46341) throw new OverflowException(); else return p*p; }Note that this code additionally throws an exception if the parameter is less than 46341.
-
Write a
Dividemethod that takes twointarguments and returns the result of dividing the first parameter by the second. If the second parameter is 0, then the method should throw anArgumentOutOfRangeExceptionexception.Solution
public static int Divide(int dividend, int divisor){ if (divisor == 0) throw new ArgumentOutOfRangeException(); else return dividend / divisor; }-
Write a piece of code (to be inserted into a
Mainmethod) that asks the user to enter one number. Then, callSquarewith it (no need to loop). Your code should not check the value of thestringentered for the operation, but must catch the exceptions potentially thrown by theSquaremethod (and, as a bonus, by theParsemethod). Your code should also display “Thanks!” regardless of whether an exception was thrown.Solution
try{ int uInput = int.Parse(Console.ReadLine()); Console.Write("Your value squared is " + Calculator.Square(uInput)); } catch(OverflowException) { Console.WriteLine("Your value squared will not fit in an int!"); } finally{ Console.WriteLine("Thanks!"); }Complete solution
using System; static class Calculator { public static int Square(int p) { if (p > 46341 || p < -46341) throw new OverflowException(); else return p * p; } public static int Divide(int dividend, int divisor) { if (divisor == 0) throw new ArgumentOutOfRangeException(); else return dividend / divisor; } } class Program { static void Main(string[] args) { try { Console.WriteLine("Enter a value"); int uInput = int.Parse(Console.ReadLine()); Console.Write( "Your value squared is " + Calculator.Square(uInput) + ".\n" ); } catch (OverflowException) { Console.WriteLine( "Your value squared will not fit in an int!" ); } // The following catch block catch exceptions thrown by int.Parse: catch (ArgumentNullException) { Console.WriteLine("No argument provided."); } catch (FormatException) { Console.WriteLine( "The string does not contain only number characters." ); } finally { Console.WriteLine("Thanks!"); } } } -
Footnotes
-
The square of is greater than the maximum value an
intcan hold,int.MaxValue. ↩