# Exercise Solutions: Exceptions¶

Line numbers are for reference. They may not match your code exactly.

## Divide by Zero¶

• Complete a function called `Divide()` in `Program.cs`. `The Divide()` method takes in two parameters: `x` and `y`.

• Your function should return the result of `x/y`.

• However, if `y` is zero, you should throw an exception.

 ``` 1 2 3 4 5 6 7 8 9 10 11``` ``` static double Divide(double x, double y) { if (y == 0.0) { throw new ArgumentOutOfRangeException("y", "You cannot divide by zero!"); } else { return x / y; } } ```
• Put your `try/catch` block in `Main()` to test out your error-handling skills. If an exception is caught, make sure to print out the error message.

 ```1 2 3 4 5 6 7 8``` ``` try { Divide(num1, num2); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.Message); } ```

Back to the exercises

## Student Test Labs¶

The `CheckFileExtension()` function should do the following:

• Take in one parameter: `fileName`.

• Return an integer representing the number of points a student receives for properly submitting a file in C#.

• If a student’s submitted file ends in `.cs`, they get 1 point.

• If a student’s submitted file doesn’t end in `.cs`, they get 0 points.

• If the file submitted is `null` or an empty string, an exception should be thrown. What kind of exception is up to you!

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18``` ``` static int CheckFileExtension(string fileName) { if (fileName == null || fileName == "") { throw new ArgumentNullException("fileName","Student did not submit any work!"); } else { if (fileName.Substring(fileName.Length - 3, 3) == ".cs") { return 1; } else { return 0; } } } ```

Back to the exercises