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
In this video, we'll review the solution to the third challenge and introduce our fourth and final challenge—throwing and catching exceptions.
Instructions
4th Challenge
-
In the MediaType.cs file...
- Throw an exception in the MediaType class constructor if the
title
parameter is set tonull
or an empty string. - Set the exception's message to "A media type must have a title."
- Throw an exception in the MediaType class constructor if the
-
In the Program.cs file...
- Throw an exception in the
DetectMediaType()
helper method when an unexpected media subtype is encountered. - Set the exception's message to "Unexpected media subtype encountered."
- Wrap the code in the
Main()
method in atry/catch
statement. - Define a parameter in the
catch
clause in order to get a reference to the caught exception. - Write the exception's
Message
property to the console.
- Throw an exception in the
Help
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Objects course.
How do it go with the third challenge?
0:00
Let's walk through my solution.
0:02
In the Program.cs file, I created a static
method named detect media type and
0:04
to find the parameter of
type media type named item.
0:10
I load the method right
below the main method.
0:14
Static void DetectMediaType.
0:21
And for the parameter, it needs to be of
type MediaType, and I'll name it item.
0:27
In the method body, I use the series
of conditional statements and
0:34
the C# is operator to identify which
particular media type subclass the item
0:38
parameter is an instance of.
0:43
So, if item, and then the is operator,
0:45
and then the type that we're testing for,
Album.
0:49
Then a set of curly braces.
0:54
Then, else if item the is operator,
then the Book type.
0:56
A set of curly braces, and
1:05
then another else if item is Movie.
1:08
And a final set of curly braces.
1:14
This series of conditionals will first
check to see if the item parameter is
1:17
an Album.
1:21
And if that's not true, it'll check
to see if the item is a Book, and
1:22
if that's not true,
it'll check to see if the item is a Movie.
1:26
After determining the MediaType
item subclass type,
1:30
I wrote a message to the console
identifying that type.
1:33
So if the item is an Album,
1:37
I wrote Console.WriteLine item.Title,
1:40
+, and then a string literal is in album.
1:48
Copy that to the clipboard,
then paste and paste.
1:54
All of this can stay the same
here because item.Titles what
2:00
we need to output, but
instead of is an album, say is a book.
2:05
Same thing down here for a movie.
2:11
We just need to change this to a movie.
2:15
And save the file.
2:20
Okay, so that completed my
DetectMediaType method implementation,
2:22
then I needed to test my changes.
2:27
To do that, I passed each of my
media type subclass instances into
2:33
a call to the detect media type method.
2:38
First, album1, so, DetectMediaType,
2:42
Then I pass album1 into the method call.
2:49
Let's copy that line of code and
paste it four more times.
2:53
I'll pass album2 into the second call.
2:57
Album3 into the third,
and then the fourth,
3:01
will get book and
the fifth method call will get movie.
3:05
And lastly, I save the file, and
I complied, and ran my program.
3:12
And here, we can see each MediaType
item subtype written to the console.
3:23
Great work so far.
3:29
Now, let's take a look at a couple
of shortcomings in our program.
3:30
First, let's see what
happens if we pass null, or
3:35
an empty string as the argument for
a media types title parameter.
3:38
Just after album3 here,
I'll create a fourth album.
3:45
So var, album4 = new Album,
and for the title,
3:48
I'll pass null instead of a value,
then for
3:53
the artist name I'll say, Some Artist.
3:57
Then I'll create a fifth album, so album5,
4:04
= new Album, and this time,
I'll pass an empty string.
4:10
And for the artist,
I'll use Some Artist again.
4:16
Let's save the file, and
compile, and run our program.
4:20
Looks like I forgot to call get
display text on album4 and album5.
4:28
The compiler is telling us that we
assigned a value to those variable but
4:34
we never used them.
4:38
So let's fix that problem, Right here.
4:40
Let's copy that line to the clipboard and
paste it twice.
4:44
Then change album3 to album4,
and album3 here to album5.
4:48
Save the file, And compile,
and run the program.
4:53
And right here in the console, we can see
that our program happily allows the null,
5:07
or empty string value in
attempts to write the album's not
5:12
existent title to the console.
5:15
Let's look at another short coming.
5:17
In the detect MediaType method, let's
comment out the last else if statement.
5:19
Then save, and compile,
and run our changes.
5:29
We can see here that the three
albums were identified.
5:37
And the book, but there's nothing
displaying whatsoever for the movie.
5:41
This tells us,
that the detect MediaType methods silently
5:45
fails to detect the media type for
the movie subclass instance.
5:48
Ideally, the program would make it more
apparent when it encounters either of
5:52
these situations, with a fourth and
last challenge in this practice session.
5:56
Let's update our program to handle both
of these unexpected situations in a more
6:01
robust manner.
6:06
In the MediaType.cs file,
throw an exception in the MediaType class
6:11
constructor if the title parameter
is set to null or an empty string.
6:16
Set the exceptions message to,
A media type must have a title.
6:21
In the Program.cs file, throw an exception
in the DetectMediaType helper
6:25
method when an unexpected
media type is encountered.
6:30
Set the exception's message to
Unexpected media subtype encountered.
6:34
Then, to finish this challenge up,
6:39
wrap the code in the Main method
in a try/catch statement.
6:41
Define a parameter in the catch clause,
in order to get a reference to the caught
6:45
exception, write the exception's
Message property to the console.
6:49
And that's it for the fourth challenge.
6:53
See you in a bit.
6:55
You need to sign up for Treehouse in order to download course files.
Sign up