# How to Think Like a Computer Scientist: The PreTeXt Interactive Edition

## Section13.4Principles for using Exceptions

There are many bad examples of exception use on the Internet. The purpose of an exception is to modify the flow-of-control, not to catch simple errors. If your try: except: block is in the same function that raises the exception, you are probably mis-using exceptions.
If a condition can be handled using the normal flow-of-control, don't use an exception!
Example 1:
Example 2:
Example 3:
If you call a function that potentially raises exceptions, and you can do something appropriate to deal with the exception, then surround the code that contains the function call with a try: except: block.
Example: Suppose you have a function that reads a file to set the state of an application when it starts up. You should catch any errors related to reading the file and set the state of the application to default values if they can't be set from the file.
try: