Handling NameError Exception in Python
Last Updated :
30 Aug, 2024
Prerequisites: Python Exception Handling
There are several standard exceptions in Python and NameError is one among them. NameError is raised when the identifier being accessed is not defined in the local or global scope. General causes for NameError being raised are :
1. Misspelled built-in functions:
In the below example code, the print statement is misspelled hence NameError will be raised.
Python
geek = input()
printf(geek)
Output :
NameError: name 'printf' is not defined
2. Using undefined variables:
When the below program is executed, NameError will be raised as the variable geek is never defined.
Python
geeky = input()
print(geek)
Output :
NameError: name 'geek' is not defined
3. Defining variable after usage:
In the following example, even though the variable geek is defined in the program, it is defined after its usage. Since Python interprets the code from top to bottom, this will raise NameError
Python
print(geek)
geek = "GeeksforGeeks"
Output :
NameError: name 'geek' is not defined
4. Incorrect usage of scope:
In the below example program, the variable geek is defined within the local scope of the assign function. Hence, it cannot be accessed globally. This raises NameError.
Python
def assign():
geek = "GeeksforGeeks"
assign()
print(geek)
Output :
NameError: name 'geek' is not defined
Handling NameError
To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.
Python
def geek_message():
try:
geek = "GeeksforGeeks"
return geeksforgeeks
except NameError:
return "NameError occurred. Some variable isn't defined."
print(geek_message())
Output :
NameError occurred. Some variable isn't defined.
Similar Reads
Handling OSError exception in Python Let us see how to handle OSError Exceptions in Python. OSError is a built-in exception in Python and serves as the error class for the os module, which is raised when an os specific system function returns a system-related error, including I/O failures such as "file not found" or "disk full". Below
2 min read
Multiple Exception Handling in Python Given a piece of code that can throw any of several different exceptions, and one needs to account for all of the potential exceptions that could be raised without creating duplicate code or long, meandering code passages. If you can handle different exceptions all using a single block of code, they
3 min read
How to handle KeyError Exception in Python In this article, we will learn how to handle KeyError exceptions in Python programming language. What are Exceptions?It is an unwanted event, which occurs during the execution of the program and actually halts the normal flow of execution of the instructions.Exceptions are runtime errors because, th
3 min read
Python Exception Handling Python Exception Handling handles errors that occur during the execution of a program. Exception handling allows to respond to the error, instead of crashing the running program. It enables you to catch and manage errors, making your code more robust and user-friendly. Let's look at an example:Handl
7 min read
Handling TypeError Exception in Python TypeError is one among the several standard Python exceptions. TypeError is raised whenever an operation is performed on an incorrect/unsupported object type. For example, using the + (addition) operator on a string and an integer value will raise a TypeError. ExamplesThe general causes for TypeErro
3 min read