April 15, 2015, 1:35 a.m. by Hayden Cooper Stites
What Are Try And Except Statements?
Sometimes you have an error in your code and you don't know how to fix it? Making a try and except statement will be very useful then. It's format is very similar to the layout of the if and else clause:try: primt 'Hello, World!" except: print "Hello, World!"
This will not generate a error in Python even though print is spelled wrong and there is two different types of quotes. What happens is that it tries to do that code and if it generates an error it moves on to the except code and does that. Try and except statements also do everything before the error. So in other words Python will declare any variables or print anything out before the error making the statement more useful.try: x = 2 + 2 primt 'Hello, World!" except: print x
A try and except statement is used when you need to run through an error. It is very useful to use.
Given: A "primt" keyword.
Return: A try and except statement using "primt"
try: primt "Nice whether today." except: print "Sorry about the delay..."