#912 – Intellisense Can Show Exceptions that a Method Might Throw

In some cases, when the Intellisense function within Visual Studio shows you information about a method that you are going to call, it will also list potential exceptions that the method might throw.

In the example below, we see that the Matrix.Invert method can throw an exception of type InvalidOperationException.

912-001

 

It’s not the case that every single method that you might call in the Base Class Library (BCL) will provide this information.  Some methods will list the exceptions that they might throw, but many will not.

#875 – Looking at the Call Stack after Catching an Exception

After an exception is thrown, it bubbles up the call stack until a handler that can handle the exception is found.  If you set a breakpoint in an exception handler, you can then use the debugger in Visual Studio to examine the call stack, as it existed at the time that the exception was thrown.

In the example below, we’ve hit a breakpoint that we set in an exception handler.  If we examine the Exception variable passed to the handler, we see that an exception of type ArgumentException has been thrown.

Notice that we can also look at the StackTrace property in the debugger.  It contains text describing the call stack at the time that the exception was thrown.  You can click on the magnifying glass icon to see the stack trace formatted in a way that’s easier to read.

In the example, we see that Dog.Bark threw the exception.

875-001

#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

#770 – Use Intellisense to Get List of Methods to Override

In a derived class, you can override a member of the base class if it’s declared as virtual.  In Visual Studio, the Intellisense feature can help you to discover the methods that can be overridden.

Suppose that we have the following definition for Dog:

    public class Dog
    {
        public virtual string Prop1 { get; set; }
        public string Prop2 { get; set; }

        public virtual void Method1()
        {
            Console.WriteLine("Dog.Method1");
        }

        public void Method2()
        {
            Console.WriteLine("Dog.Method2");
        }
    }

Now when we are adding code to a child class of Dog, we can just enter the keyword override, followed by a space, to see a list of candidate members that can be overridden.

770-001

To add the code for one of these members, just use the down arrow to select the method and then press TAB.  The body of the method will be generated for you.

770-002

#766 – Adding an Interface to a Class Diagram

If you add a class to a class diagram and the class implements one or more interfaces, you’ll see the interfaces show up on the class with the standard interface designator (a short line with a circle at the end).

766-001

 

You can also add an element to the class diagram that represents the interface itself.  To do this, just right-click on an interface and select Show Interface.

766-002

 

Once you do this, the interface will be added to the diagram.  You can then expand the basic element to see the members of the interface.

766-003

#765 – Adding Base or Derived Classes to a Class Diagram

You can use the class diagram to follow the inheritance chain for any class currently in the diagram, moving up to the class’ base class or down to the classes that derive from the class.

For example, let’s say that you’ve dragged the ArgumentException class onto a class diagram, from the Class View.

Capture

 

You can add the base class of ArgumentException to the diagram by right-clicking on the class and selecting Show Base Class.

765-002

 

The base class will now be shown on the diagram.

765-003

 

You can also show all derived classes of a particular class by right-clicking on the class and selecting Show Derived Classes.

765-004

 

Using this technique, you can easily browse through the classes in the .NET Framework.

765-005