From the course: Using Python for Automation

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Automate error handling

Automate error handling - Python Tutorial

From the course: Using Python for Automation

Automate error handling

- Welcome to the world of error handling where each bug is a puzzle waiting to be solved. In this lesson, you'll dive into the crucial aspect of programming that deals with detecting and resolving errors that occur when a program is executed. Let's explore a couple of examples for how to automate error handling in Python. Open up automate_error_handling_part1.py and automate_error_handling_part2.py from the Exercise Files folder. Let's start with automate_error_handling_part1.py. In this example, the goal is to get the user to input a number and perform a calculation with that number. However, there are two potential errors that you could get, the user might enter a non-number value or they might enter zero as the input. To handle these errors gracefully, you use a try-except block. The try block contains the code that might raise an exception. So if the user enters a value that isn't a number, a ValueError will occur. And if the user enters zero, the division operation will raise a…

Contents