Python in 7 Days — 07/7:Exceptions & How to Handle Then
Python has many built-in exceptions that you can use in your programs. When an error occurs, an exception is raised.
You can handle exceptions in your code using the try and except statements.
The try statement allows you to test a block of code for errors.
The except statement lets you handle the error.
You can also use the finally statement to execute code, regardless of the result of the try- and except statements.
Errors and Exceptions
Python errors and exceptions are important to understand because they can drastically improve your code quality and debugging speed.
Errors
Errors are the most common type of problem you’ll encounter when you’re first starting to code. They’re also the easiest to fix.
There are three types of errors in Python:
Syntax errors
Runtime errors
Logic errors
Syntax errors are the most basic type of error. They occur when you make a mistake in the structure of your code.
For example, you might forget to close parenthesis or forget to indent a line of code.