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
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video, we'll review the solution to the fourth and final challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
<noob />
17,062 Points3 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
How did you do?
0:00
Let's take a look at my solution.
0:01
I started in the MediaType.cs file.
0:03
In order to throw and exception in
the MediaType class constructor,
0:07
if the title parameter is set to null or
an empty string, I added an if statement.
0:11
So if () {}, and then for the condition,
0:16
I added a call to
the string IsNull orEmpty
0:21
method passing in the title parameter.
0:25
So, if the string is null or
empty method returns true,
0:31
meaning that the title
parameter is null or
0:35
empty, I added code to throw
an instance of the Exception class.
0:38
So throw new Exception.
0:43
The Exception type is in
the system namespace, so
0:49
let's add a using statement for
that namespace, using System.
0:53
To set the exceptions message,
I passes the string literal,
1:01
A media type must have a title,
1:06
Into the call to the constructor,
I save the file and switch to Program.CS.
1:13
In order to throw an exception in
the detect media type helper method,
1:19
when an unexpected media
subtype is encountered,
1:24
I added an else statement right here,
after each of my else if statements.
1:27
I'll go ahead and
leave this last else if commented out, so
1:37
when I test my changes,
it won't detect the movie subclass type.
1:41
So else, A set of curly braces.
1:45
Then with the else statement code block,
1:51
I added code to throw an instance of
the exception class, throw new Exception.
1:53
To set the exceptions message,
I passed the string literal, Unexpected,
2:00
Media subtype encountered,
Into the call to the constructor.
2:06
Then, I wrap the code in the main
method in a try catch statement.
2:16
Scroll all the way up here to the top, and
2:23
just inside of the main method,
add try, a set of {},
2:27
Then catch, (), and another set of {}.
2:34
Then, I selected all of the rest
of this code in the method,
2:42
cut it to the clipboard, and
2:47
pasted it in between the curly
braces following the try keyword.
2:49
I fixed the indentation of my code by
selecting it, And pressing the tab key.
2:57
That helps with readability.
3:05
Then, I defined a parameter in
the catch clause in order to get
3:10
a reference to the caught exception.
3:13
So, Exception for the exception type,
and ex for the parameter name.
3:16
Then, within the catch code block,
I use the console right line method to
3:23
write the caught exceptions
message property to the console.
3:28
Console., Writeline, Exception:,
3:32
close the string and then + ex and
then the Message property.
3:38
And lastly, I saved the program.cs
file and compiled, and ran my program.
3:46
And here's our first exception message,
A media type must have a title.
3:55
That's being thrown by the media
type base class constructor,
4:08
because we're passing null for
the title for this album.
4:11
Let's remove that line of code, and
the subsequent reference down here.
4:15
And compile and run our application again.
4:22
And again,
we get a media type must have a title.
4:27
Now that's a result of passing an empty
string for the title for this album.
4:31
Let's remove that line of code and
the subsequent reference down here.
4:35
Save, compile, and
run the application again.
4:40
Now we're seeing the three album types
identified followed by the book type.
4:45
But then, we're seeing a message that an
unexpected media subtype was encountered.
4:49
To fix this issue, we simply need
to uncomment these lines of code.
4:55
So the movie subclass type
can be correctly identified.
5:00
Save the file, and compile,
and run our application again.
5:05
And now we're seeing all
of the expected output.
5:11
Sometimes the console gets a little
confused when displaying output.
5:17
If that happens, type the clear command,
5:20
Then rerun the previous command by
pressing the arrow key up twice.
5:24
Now, we can see all of
the expected output.
5:30
Nice job completing this practice session.
5:36
Be sure to check out the next
session in the series
5:39
after you've completed the fourth
stage in the C# objects course.
5:41
Thanks for practicing with me,
and we'll see you next time.
5:45
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up