Creating the main code
Start with a blank C# console app, and on the first line type the following:
using System;
We need this for exceptions. Remember, exceptions are usually predictable error situations that can occur in your code, and you should be able to handle them. That's why, for example, the .NET framework that Microsoft has created contains a collection of libraries or classes to handle various types of predictable errors well.
We'll begin by creating our standard program entry point; for this, type the following:
class Program { static void Main() { } }
What we are going to do is read two values and try to divide them. So, inside our Main
function block, type the following:
double x = 5, y = 0;
These are the values we will try to divide, and we'll do it in this order: x/y
. So, you can probably see what kind of error we're going to experience. Underneath this, we'll set up our try
/catch
block:
try { } catch() { }
Throwing an exception
We're going to try to divide...