Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We can configure how the debugger behaves when exceptions are thrown.
Just My Code is a setting in Visual Studio that will cause the debugger to step over code that is not yours (e.g. System and Framework code). This allows us to focus on the code that we’ve written instead of being forced to step through all the abstraction layers. This setting will also simplify the output in the Call Stack Window by only showing us entries from our code and collapsing any external code.
If an exception is thrown
while debugging and
0:00
nothing handles it., the debugger
breaks and alerts us to the condition.
0:02
Let's take a deeper look at how Visual
Studio helps us to debug exceptions.
0:07
Let's say for example, that we forgot to
initialize the song variable up here.
0:12
Now let's start debugging.
0:19
I'll enter option 2 here.
0:23
When the program gets here,
it throws an exception
0:24
because songs is null, and we can't call
the for each method on a null variable.
0:30
When the debugger pauses as
a result of an exception,
0:36
we get this pop up that contains
a bunch of additional information.
0:39
Here we can see the exception name,
and here,
0:43
right beside additional information,
we can see the exception's message.
0:46
It says, object reference not
set to an instance of an object.
0:50
Visual Studio provides
some debugging tips for
0:54
many of the most common exception types.
0:56
We can see even more
information about the exception
0:59
by clicking on View Details down here.
1:01
Here we see the contents
of the exception object.
1:05
This often has a lot of
really useful information.
1:08
Here we can see if there
was an inner exception or
1:11
an exception that caused this exception.
1:14
We can also see the stack trace up to
the point where the exception was thrown.
1:17
This is often different than
the stack trace that is shown
1:22
in the call stack window.
1:25
Because the exception could have
been thrown from external code, and
1:26
where the debugger has paused is just the
first place where it is propagated back
1:31
to our code.
1:34
Visual Studio will pause the debugger for
all unhandled exceptions.
1:36
But what about exceptions
we have handlers for?
1:40
Sometimes we still want to
break at the point where these
1:44
exceptions are thrown too.
1:47
Let's wrap this line by
adding a try catch around it.
1:48
We'll go ahead and handle all exception
types by catching System.exception.
2:00
And I'll just do nothing here.
2:09
I should mention that catching
all exception types and
2:13
doing nothing is a really
bad programming practice.
2:16
But again, this is just for
demonstration purposes.
2:19
Now let's start the debugger and
see what happens.
2:22
Again, I'll hit option two here.
2:28
Because we are now handling the exception,
2:31
the debugger no longer breaks
when the exception is thrown.
2:33
We can tell it to continue breaking by
going into the exception settings window.
2:36
If the exception settings
window isn't already open,
2:41
you can open it from
the debug menu under Windows.
2:43
You can also often find it down here.
2:46
Here we see many categories of exceptions.
2:48
Because this is a C Sharp program,
2:52
we're mostly interested in
the Common Language Runtime exceptions.
2:54
These other exception types
don't apply to us right now.
2:58
We can scroll down here and
find the null reference exception,
3:01
or we can just type system.null,
and it will find it for us quickly.
3:06
By clicking on this checkbox, we're
telling Visual Studio to break on this
3:13
exception, even if it is handled
at some point in the code.
3:17
Now if we restart the debugger and
enter option 2 again,
3:20
we'll see that it breaks on
line 42 of program that CS.
3:24
Notice that this list only contains
exceptions provided by the .Net framework.
3:32
We can add our own custom
exceptions by right clicking
3:36
on Common Language Runtime Exceptions,
and clicking Add Exception.
3:40
Now we can add our own exception
type with its full namespace name.
3:43
We don't have any custom exceptions
in our program so far, but
3:47
this is a good thing to be
aware of in the future.
3:50
There are a keyboard shortcuts for just
about everything we've talked about here.
3:54
Often times using a keyboard
can make coding debugging and
3:58
navigating around the code
much more efficient.
4:02
Check out the link in the teacher's
notes for a list of the fall short cuts.
4:05
I hope you feel much more comfortable
debugging in Visual Studio.
4:08
I think you'll find having a good
debugger at your fingertips can
4:12
make it a lot easier to track down pesky
bugs the crop up from time to time.
4:15
Until next time, happy coding.
4:19
You need to sign up for Treehouse in order to download course files.
Sign up