Concept Of Python Error Keywords With Practical Examples

In this article you will learn about Python Error Keywords Try, Except, Finally.

Python Error keywords Before moving ahead, let’s know a little bit about Python Python if-else Statement

Python keywords – Python keyword is defined as some reserved keywords that are owned by Python and can only be used in some specific condition as they are set to be used. These keywords cannot be used in any other condition except its main condition for which it is used.

Note: Python keywords cannot ever use to declare a variable name.

Try – It tests whether a block of code contains an error or not.

Except – It handles errors. It executes except block of code when only try block of code is false.

Finally – It executes code, regardless of the answer of the try-except condition.

Example 1- Check whether the code contains an error or not.

try:
   print(z)
python-error-keywords

As it is seen clearly that there is no exception clause therefore, it returned an error.

Example 2- Check whether the code contains an error or not.

try:
    print(z)
except:
    print('No problem') 
python-error-keywords

It has been noticed that ‘z’ is not defined, but as except keyword is used here to handle the error, it executed the print statement.

Note: Since the try block is false, Python interpreter executed except block. Without try block program will return an error.

Example 3- Raised an error without try the keyword.

print(x)

It will return an error as a result of the exception is not here to handle the error.

Python Exception – It allows us to use as many exceptions as per want. You can modify an error result according to what you want.

Example – Modify an error message.

try:
    print(z)

except NameError:
    print('No problem')

except:
    print("It is an exceptional.") 
python-error-keywords

As a result, the exception message is modified by mentioning the error name, therefore, it returned a specified exception statement.

Else – Use the else keyword to execute a block of code or message if no error is raised in the try block of code.

Example 4- Execute else block of code, if try block of code does not raise an error.

try:
    z = 10
    print(z)

except NameError:
    print('No problem')

else:
    print('It will execute if try not raises an error.') 
python-error-keywords

As a result, try block of code didn’t return any error, therefore, else block of code executed.

Finally – Use finally keyword to execute a block of code or message when it doesn’t matter whether try block of code returns an error or not.

Example 5- Execute finally block of code, whether try block of code raises an error or not.

try:
    print(z)

except NameError:
     print('It is exception block of code')

finally:
     print('It will always execute') 
python-error-keywords

As shown above that finally block of code will execute no matter whether try block of code raises an error or not.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

1 thought on “Concept Of Python Error Keywords With Practical Examples”

Leave a Comment

Stay in the loop

codingstreets