Keywords
try: - This initiates the block of code that the program will try to run.except: - This keyword can be followed by an exception type, or if left blank will capture all exceptions. This is followed by a block of code that should be ran if the program raises an exception that matches the exception type stated.
else: - This runs if no errors are raised.
raise: - This keyword allows you to create your own exceptions allowing you to tailor the response your code has to an exception depending on where the program has got to.
finally: - This block of code will run once the try block or the except block has completed.
Example
If we try to run the following code we get an error raised.a = 3 b = "chair" c = a / b
Traceback (most recent call last):
File "C:/Python3/errors.py", line 4, in <module>
c = a / b
TypeError: unsupported operand type(s) for /: 'int' and 'str'
a = 3 b = "chair" try: c = a / b except: print ("Error dividing, please check you have two numbers and try again")
Error dividing, please check you have two numbers and try again
a = 3 b = "chair" try: c = a / b except TypeError: print ("Error dividing, please check you have two numbers and try again")
Error dividing, please check you have two numbers and try again
a = 3 b = "chair" try: c = a / b except TypeError: print ("Error dividing, please check you have two numbers and try again") else: print ("Else block ran") finally: print ("Final block ran")
Error dividing, please check you have two numbers and try again Final block ran
a = 3 b = 9 try: c = a / b except TypeError: print ("Error dividing, please check you have two numbers and try again") else: print ("Else block ran") finally: print ("Final block ran")
Else block ran Final block ran
class ExceptionVariable9(Exception): def __init__(self): Exception.__init__(self) a = 3 b = 9 try: if b == 9: raise ExceptionVariable9() c = a / b except ExceptionVariable9: print ("Error dividing, You are trying to divide by nine!") except TypeError: print ("Error dividing, please check you have two numbers and try again") else: print ("Else block ran") finally: print ("Final block ran")
Error dividing, You are trying to divide by nine! Final block ran
Related Sections
Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.
Related Pages
Pickle - The pickle library allows a program to save data in a binary file.Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines