#850 – Three Types of Errors that Can Lead to Exceptions

An exception is an event that occurs at run-time and is not part of the normal expected execution of your code.

There are different scenarios that can lead to an exception occurring while your application is executing.  They can be broken down into the following three categories:

  • User errors
  • Runtime errors
  • Bugs

user error is anything that the user does that you did not expect him to do.  For example, you might ask the user to enter his age and he enters “Bob” instead.

runtime error is an error that occurs as a result of something going wrong within one of the methods in the .NET Framework.  For example, you might run out of disk space while trying to write a file.

bug is a mistake that you make in your source code, which may lead to an exception.

#849 – Using the Call Stack in Visual Studio to Navigate within Your Code

When in break mode within Visual Studio, you can view the call stack in the Call Stack window.

When you bring up the Call Stack window, there will be a yellow arrow pointing to the top of the call stack, indicating the location of the next statement that will execute when you resume execution.  The Locals window will show the values of local variables within the current method.

849-001

At this point, you can double-click on another method within the call stack.  When you do, the code editor will show code for that other method and the Locals window will show local variables for the same method.  They will have the values that existed at the point that the method at the top of the call stack was called.  Notice that a yellow arrow still indicates the execution point, but a green arrow now appears, showing the current method being examined.

849-002

#848 – Viewing the Call Stack in Visual Studio

The call stack keeps track of the currently executing method in your application, and from where that method was called.  You can use the debugger in Visual Studio to view the current call stack when you are in break mode (at a breakpoint or stepping through your code).

If the call stack is not already visible, click on the Debug menu, then Windows and Call Stack.  (Or press Ctrl+D, C).

848-001

In the example below, we’ve started the application and are located in our Main method, which was called by native code.

848-002

If we now step into the Dog.BarkYourAge method, the call stack shows this method on the top of the stack, with Main as the second entry.  Main is just below BarkYourAge in the stack, because it called BarkYourAge.

848-003

If BarkYourAge then calls DogUtil.GenerateBark and we step into that method, we see:

848-004

If we return from GenerateBark, we see:

848-005

#847 – How the Call Stack Works

A call stack is an indication of which method in your application is currently executing and how the application got there.  When a method is called, information about that method is pushed onto the call stack.  When the method returns to its caller, that information is popped back off the stack.  In this way, the top of the stack always refers to the current method.  And, traversing down the stack, we see the sequence of calls that got us to the current method.

For example, assume that when your application starts, a Main method is called.  Information about Main is pushed onto the stack.

847-001

Assume that Main then creates an instance of a Dog object and calls its BarkYourAge method.

847-002

If BarkYourAge then calls method DogUtil.GenerateBark, we get:

847-003

When control returns from GenerateBark, we continue in the BarkYourAge method and the stack becomes:

847-002

#846 – A Call Stack Keeps Track of Methods that Have Been Called

When your application is running, Windows uses a stack data type to manage its call stack.  A call stack is essentially just a record of the currently active methods.  At the top of the stack is the current method that is executing.  Below this method in the stack is the method that called the current method.  At the bottom of the stack is the first method that began executing when your application started.

For example, if you have a Main method that calls method Dog.BarkYourAge and this method in turn calls a method named DogUtil.GenerateBark, the call stack will appear as follows during executing of GenerateBark.  (For simplicity, we’re ignoring the .NET Framework methods that are called before invoking Main).

846-001

When a method is called, it is pushed onto the stack.  When it returns to its calling method, it is popped off of the stack.

#845 – Using the Generic Queue Class

A queue is a data structure that allows adding elements at one end of the queue, through Enqueue operations, and removing elements from the other end, through Dequeue operations.

The .NET Framework includes a Queue<T> type, defined in System.Collections.Generic, that implements a queue data type.

When you declare a queue, you indicate the type of objects that the queue will contain.

            Queue<Person> peopleInLine = new Queue<Person>();

You can now add instances of Person objects to the (back of) the queue using the Enqueue method.

            // Ernest - 1st guy in line
            peopleInLine.Enqueue(new Person("Ernest", "Hemingway"));

            // Scott in line behind Ernest
            peopleInLine.Enqueue(new Person("F. Scott", "Fitzgerald"));

845-001
You can remove the object from the front of the queue using the Dequeue method.

            // Remove guy from front of line -- Ernest
            Person next = peopleInLine.Dequeue();

845-002

#844 – The Queue Data Type

A queue is a data type used to store a collection of items and which has the following properties:

  • Elements can be added to the queue
  • Elements can be removed from the queue, but only in the order in which they were added

Adding an item is known as a Enqueue operation.  Removing an item is known as a Dequeue  operation.

You can think of a queue data type as being like people standing in a line.  You can enter the line only at the back (Enqueue) and you can leave the line, but only from the front (Dequeue).

A queue is also known as a FIFO (First-In, First-Out) structure.

844-001

Follow

Get every new post delivered to your Inbox.

Join 270 other followers