Skill - Handling errors in python
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
In this post, we will learn to handle errors in our python programs
Catching error using ‘except’
try:
# use mkdir function to create the folder
print(int('a2'))
except:
print('An error occured...')
In the above example, we used try except to catch the error
Catching error using ‘except’ along with error information
try:
# use mkdir function to create the folder
print(int('a2'))
except Exception as e:
print(e)
# this prints
# invalid literal for int() with base 10: 'a2'
In the above example, we catch the error in a variable e
and print it
Catch error based on the error type
try:
print(x)
except NameError:
# handle if the error is of type `NameError`
print("Name error occured, which means that, a variable name is not defined")
except OSError as err:
# handle if the error is of type `OSError`
print("OS error: {0}".format(err))
except:
# handle any other error types
print("Something else went wrong")
# the above program prints "Name error occured, which means that, a variable name is not defined"
In the above example, first we check if the error type is NameError
and it.
Otherwise we check if the error is of type OSError
and handle it.
After that we catch any other types of errors.
Using ‘finally’ keyword for must run code
try:
print(x)
except Exception as e:
print("Error info: {0}".format(e))
finally:
# using finally to run code regardless of errors in 'try' section
print("This code runs regardless of errors")
# The above code prints
# Error info: name 'x' is not defined
# This code runs regardless of errors
Create custom exception using ‘raise’
x = -2
if x < 0:
raise Exception("Numbers less than zero are not allowed")
# the above code throws an error
Online Interpreter
Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://www.tutorialspoint.com/execute_python_online.php
References
- https://stackoverflow.com/questions/4990718/about-catching-any-exception
- https://www.w3schools.com/python/python_try_except.asp
Comments
Post a Comment