Basically, you cannot, or at least not in a simple and reliable way. As you noted, AttributeError is a mechanism used by Python to determine if an attribute is found in regular places. Although the __getattr__ documentation does not mention this, the documentation for __getattribute__ , as described in this answer , makes it more clear that you are already associated with.
You can override __getattribute__ and catch AttributeError there, but if you caught it, you wonโt have an obvious way to find out if it was a โrealโ error (this means the attribute was not really found) or an error caused by the user code during the search process . Theoretically, you can check the trace in search of specific things or make other other hacks to try to verify the existence of the attribute, but these approaches will be more fragile and dangerous than the existing behavior.
Another possibility is to write your own property-based descriptor, but catch attributes attributes and re-create them as something else. This, however, will require you to use this property replacement instead of the built-in property . In addition, this would mean that AttributeErrors attributes created from internal descriptor methods are not propagated as attribute attributes, but like something else (whatever you replace them with). Here is an example:
class MyProp(property): def __get__(self, obj, cls): try: return super(MyProp, self).__get__(obj, cls) except AttributeError: raise ValueError, "Property raised AttributeError" class A(object): @MyProp def a(self): print "We're here -> attribute lookup found 'a' in one of the usual places!" raise AttributeError return "a" def __getattr__(self, name): print "We're here -> attribute lookup has not found the attribute in the usual places!" print('attr: ', name) return "not a" >>> A().a We're here -> attribute lookup found 'a' in one of the usual places! Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> A().a File "<pyshell#6>", line 6, in __get__ raise ValueError, "Property raised AttributeError" ValueError: Property raised AttributeError
Here AttributeErrors are replaced by ValueErrors. This may be good if you only want to make sure that the exception is "breaking out" of the attribute access mechanism and can be propagated to the next level. But if you have a complex exception code that expects to see AttributeError, it will skip this error because the type of exception has changed.
(Also, this example obviously deals only with property attributes, not setters, but it should be clear how to extend the idea.)
I suggest that as an extension of this solution, you could combine this MyProp idea with a custom __getattribute__ . Basically, you can define a custom exception class, say PropertyAttributeError , and have a remark of replacing AttributeError properties with PropertyAttributeError. Then in your custom __getattribute__ you can catch a PropertyAttributeError and raise it again as an AttributeError. Basically, MyProp and __getattribute__ can act as a "shunt" that bypasses normal Python processing, converting the error from AttributeError to another, and then converting it back to AttributeError again as soon as it is "safe" for this. However, I feel that this is not worth it, as __getattribute__ can have a significant impact on performance.
One small addition: a bug about this was raised in Python, and recently there has been information about possible solutions, so it may be possible to fix it in future versions.