Ok, while we are here, time for exceptions.
Trust me, if you were crying over how much easier your life would be now if you had known about enums from the start, might as well grab a gun now.
Let's make a simple program to take an array of people and print their names.
class Program
{
static void Main(string[] args)
{
Person[] people = new Person[3];
people[0] = new Person("Adam");
people[1] = new Person("Brain");
people[2] = new Person("Charlie");
foreach (Person p in people)
{
Console.WriteLine(p.Name);
}
Console.ReadKey();
}
}
class Person
{
private string name;
public Person(string name)
{
this.name = name;
}
public string Name
{
get
{
return name;
}
}
}
Run that, see how it works, enjoy!
But wait, we forgot somebody! Emily wants to be in our array, so let's add her.
static void Main(string[] args)
{
Person[] people = new Person[3];
people[0] = new Person("Adam");
people[1] = new Person("Brain");
people[2] = new Person("Charlie");
people[3] = new Person("Emily");
foreach (Person p in people)
{
Console.WriteLine(p.Name);
}
Console.ReadKey();
}
Try running that, what happens? Well things break. If you are working in visual studio then "people[3] = new Person("Emily");" will be highlighted and you will be getting some sort of message saying "IndexOutOfRangeException was unhandled", well what the fuck does that mean? Is the computer sexist? No, just logical, a different
kind of discrimination!
We all know that a Person[3] array can only hold 3 people, starting at 0, that means 2 is the highest value it should go to. When you try to go beyond that, it 'throws' an exception.
An exception is what happens when something goes wrong, or breaks the rules in some way, and if you don't handle them then they crash your program. Luckily for us, when an exception is thrown, it can be 'caught' before it escapes the scope of your program all together and fucks some shit up. Let's catch an exception!
static void Main(string[] args)
{
Person[] people = new Person[3];
people[0] = new Person("Adam");
people[1] = new Person("Brain");
people[2] = new Person("Charlie");
try
{
people[3] = new Person("Emily");
}
catch
{
}
foreach (Person p in people)
{
Console.WriteLine(p.Name);
}
Console.ReadKey();
}
}
Hey look, it runs again! When we tried to add Emily, it broke, but then we caught the exception and things kept going.
Now the first thing to notice is the try{} block. Every catch starts with a try. Basically your catch block will
only catch exceptions that happen inside it's try block, so for example.
people[0] = new Person("Adam");
people[1] = new Person("Brain");
people[2] = new Person("Charlie");
people[3] = new Person("Emily");
try
{
}
catch
{
}
Because the exception happens outside the try block, it does not get caught, and the program crashes. We can also put multiple things inside the try block.
try
{
people[0] = new Person("Adam");
people[1] = new Person("Brain");
people[2] = new Person("Charlie");
people[3] = new Person("Emily");
}
catch
{
}
Once again, run this, and you will find it working as you would expect by now.
Now, Brian is a pushy little SOB and wants to be added to the array first. He dosn't care what position he ends up as in the array, as long as he is the first in. Let's shut this baby up.
try
{
people[1] = new Person("Brain");
people[0] = new Person("Adam");
people[2] = new Person("Charlie");
people[3] = new Person("Emily");
}
catch
{
}
Once again, the program works the exact same way, no change, and all is fine in the world.
Now Emily decides
she wants to go first in the array! As if she hasn't caused enough problems, seriously, she is getting annoying. But hey, her name hasn't shown up in our program yet, so we owe her this much. Move her up.
try
{
people[3] = new Person("Emily");
people[1] = new Person("Brain");
people[0] = new Person("Adam");
people[2] = new Person("Charlie");
}
catch
{
}
Now run that and... OH FUCK YOU EMILY! All you do is cause issues! What the fuck now?
Ok, so this time we are getting a null pointer exception. Those show up when you are trying to access something with no data in it yet. Why is there no data? We added Adam, Brian and Charlie, right? Well while the program is still running, hover your mouse over people, and click the little plus sign. This will show you what people contains (By the way, this works for all types, not just arrays, it is fucking awesome), like so.
Hey look, all our elements are null! Adam, Brian and Charlie never made it into the array? This is because once an exception is thrown, it breaks out of the try block, and into the catch block, and then goes from there, any lines of code that are inside the try block after the exception are ignored. This is why you can't just surround your entire program in a try catch block, as soon as something goes wrong, it will smash a sledge hammer through your walls of code, and it will be just as good as crashing for real.
Now, let's put Emily back in her place, where the damage she did was minor. But also, what if we want something to happen
only if an exception was thrown? Well then we can put that code inside the catch block!
try
{
people[1] = new Person("Brain");
people[0] = new Person("Adam");
people[2] = new Person("Charlie");
people[3] = new Person("Emily");
}
catch
{
Console.WriteLine("Sorry, clubs full!");
}
Try that with and without Emily. When you try to add the fourth element to the array, an exception is thrown, caught, and then the catch function is called. Otherwise it skips over the catch block and continues on.
Continues in part 2....