Python AttributeError Exception
Example
An AttributeError
will occur if you try to access a method that does not exists:
x = "Hello"
print(x.toUpperCase())
Try it Yourself »
Definition and Usage
The AttributeError
exception occurs when you try to execute a property or method that does not exist on the current object.
You can handle the AttributeError
in a try...except
statement, see the example below.
Exception Handling
Example
Handling the AttributeError
in a try...except
statement:
x = "Hello"
try:
print(x.toUpperCase())
except AttributeError:
print("Oops!
Strings do not have a toUpperCase method!")
except:
print("Something else went wrong")
Try it Yourself »