This is a file intended to teach students that are new to programming how to handle errors in python; such as try and catch statements.

This is an example of plotting a barplot using seaborn such that the data being used is filtered by views above 3000 and the user decides what type of data they want to view on the generated barplot! plt.xticks with rotation = 85 allows for the plot to have the video titles be slanted to more easily fit.

This will print a BarGraph! The user can choose between which metric you want to plot! options: Video publish time, Subscribers lost, Subscribers gained, Watch time (hours), Impressions, or lastly Impressions click-through rate (%). The user is instructed to attempt to type "Subscribers gained". Here you can see the use of plt.pause(5) which specifies that I want to pause for 5 seconds before allowing the code to continue and closing the graph.

Wouldn't it be nice to be able to prevent this kind of error when a user types something not allowed as input or when data that used to exist cant be read anymore such as a fif file? Error handling is the solution to this! In the next cell I provide the same def plotBarGraph(userInput) but this time adjusted to show how you can handle errors safely to prevent crashes!

So in summary use try when you want to have a condition be attempted where a crash is possible, and use except when you had a condition that could have resulted in a crash under certain conditions but is prevented from crashing by providing another escape route for the program to continue on without failing. Lastly as an added bonus, there is also the finally statement which is similar to how an if -elif - else statement block however regardless if the code chooses to go to either the try-except blocks, the code will always execute the finally block regardless if one or the other occured, rather than in if - elif - else statements where only one is allowed to be executed. The way to duplicate this otherwise would be to if-else if which can become clunky if needed to be repeated.