You've probably seen your programs quit for many reasons since you started your programming journey. And you know by now that that's perfectly normal and nothing to worry about. An important part of software development is that you get familiar with error messages.
Two Types of Python Errors
Two common types of errors that Python has been telling you about are syntax errors and exceptions:
1. Syntax Errors
These are caused by a mistake in your code. Your program will never even start running unless you fix your code so that the syntax is correct.
2. Exceptions
These are caused when something unexpected occurs during runtime. For example, if you ask your users to enter a number to perform a calculation, but they enter a string instead. Exceptions will terminate your running application unless you handle them.
What You'll Learn
In this section of your course, you'll learn how to handle a Python exception. You'll know when to catch them to avoid quitting your program when you don't want to, and raise them when you want to ensure that the program doesn't continue running if something unexpected occurs. Later, you'll also learn how to write your own exceptions.
Built-In Exceptions
Python comes with built-in exceptions that you can use to make your applications more secure.
In the following lessons, you'll explore some of the ways how you can incorporate exceptions and exception handling into your applications.
Key Points
You'll learn about:
- Catching exceptions with
tryandexcept - Different variations of exception handling
- The
elseandfinallykeywords
- Raising exceptions
- Custom exceptions
Getting a handle on Python exception handling opens up an additional way to control the flow of your applications and adds another important software development tool to your growing skillset.
While you're learning the basics of exception handling in this section, you can get additional practice by incorporating exceptions into your previous project, for example, into the web scraping script and CLI application you built in the previous section.
Summary: Introduction Python Exceptions
- Python exceptions are errors in your program
- Syntax errors and exceptions are the two types
- Syntax errors are errors in your code, and your program won't start with them
- Exceptions occur during runtime when something unexpected occurs
- Python comes with built-in exceptions