Table of contents
1.
Introduction
2.
What is NameError Exception?
3.
Types of NameError Exception
3.1.
1. Misspelled built-in functions
3.2.
2. Use of undefined variables
3.3.
3. Defining variable after usage
3.4.
4. Incorrect usage of scope
4.
Handling NameError
5.
Frequently Asked Questions
5.1.
What is an exception in Python? 
5.2.
What is the NameError exception?
5.3.
How can you handle NameError in Python?
6.
Conclusion
Last Updated: Oct 8, 2024
Easy

Handling NameError Exception in Python

Author Nagendra
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An exception is a condition that arises during the execution of a program that causes the normal flow of instructions to be disrupted. When a method encounters an error, it produces an object and passes it to the runtime system. In Python, there are various standard exceptions, and NameError is one of them. When the identifier being referenced isn't declared in the local or global scope, a NameError is thrown.

Handling NameError Exception in Python

You can also read about the Multilevel Inheritance in Python and Convert String to List Python.

What is NameError Exception?

A NameError exception in Python occurs when the code attempts to use a variable or function that has not been defined or is not accessible in the current scope. This error typically happens due to typos, forgotten declarations, or attempting to access a variable before it's assigned.

Types of NameError Exception

1. Misspelled built-in functions

The following code illustrates the occurrence of an exception due to misspelled built-in function :

x=5
Print(x) #python is case sensitive , it raises exception
You can also try this code with Online Python Compiler
Run Code

 

Output

NameError: name 'Print' is not defined

2. Use of undefined variables

The following code illustrates the occurrence of exceptions due to the usage of undefined variables :

x=5
Print(y) #Here, the variable y is not defined
You can also try this code with Online Python Compiler
Run Code

 

Output

NameError: name 'y' is not defined

3. Defining variable after usage

In the following code snippet, even the variable x is defined, but it is defined after it is used in the following example. This will cause a NameError since Python interprets the code from top to bottom.

print(x)
x=5
You can also try this code with Online Python Compiler
Run Code

 

Output

NameError: name 'x' is not defined

4. Incorrect usage of scope

In the following code snippet, the variable x is defined within the local scope of the assigned function. As a result, it can't be accessed from anywhere in the world. This results in a NameError.

def val():
    x=5
val()
print(x)
You can also try this code with Online Python Compiler
Run Code

 

Output

NameError: name 'x' is not defined

Handling NameError

In Python, the NameError is handled by mentioning it in the except block. If only the NameError is triggered in the try block in the following sample code, an error message will occur.
The following code illustrates handling NameError in Python :

def val():
    try:
        x=5
        return y #here the variable y is not defined
    except NameError:
        return "NameError Occurred"
 
print(val())
You can also try this code with Online Python Compiler
Run Code

 

Output

NameError Occurred

 

You can practice by yourself with the help of online python compiler.

Check out Escape Sequence in Python

Frequently Asked Questions

What is an exception in Python? 

An exception is a condition that arises during the execution of a program that causes the normal flow of instructions to be disrupted.

What is the NameError exception?

In a code snippet, when the identifier being referenced isn't declared in the local or global scope, a NameError is thrown.

How can you handle NameError in Python?

In Python, the NameError is handled by mentioning it in the except block. If only the NameError is triggered in the try block in the following sample code, an error message will occur.

Conclusion

In conclusion, handling a NameError exception in Python involves ensuring that all variables and functions are properly defined and accessible before use. By carefully checking for typos, correct variable initialization, and appropriate scope, you can prevent and effectively manage NameError exceptions, leading to more robust and error-free code.

Check out this article:

 

We hope that this blog has helped you enhance your knowledge regarding Handling NameError Exceptions in Python and if you would like to learn more, check out our articles on Python. Do upvote our blog to help other ninjas grow.

Live masterclass