[Tutor] Checking for custom error codes

Tiger12506 keridee at jayco.net
Wed Aug 8 04:57:39 CEST 2007


> Traceback (most recent call last):
>  File "domainspotter.py", line 150, in <module>
>    runMainParser()
>  File "domainspotter.py", line 147, in runMainParser
>    td.run()
>  File "domainspotter.py", line 71, in run
>    checkdomains.lookup()
>  File "domainspotter.py", line 108, in lookup
>    from rwhois import WhoisRecord, NoSuchDomain
> ImportError: cannot import name NoSuchDomain
>
> Maybe I need to import something else to be able to throw it.
>
> I think if someone can explain a more general form of this I would be on 
> better footing: To use a custom error code (from a module) in a loop or 
> anywhere else, do I need to import the code itself? I had assumed that 
> once I imported the module that defined the error code, I could catch it 
> just like a core Python error code.


The problem is evident. The name NoSuchDomain is not defined. It is not a 
variable. It is just a string. When you call an error like this raise 
'NoSuchDomain', it's called something - don't know what (string 
exception?) - but as you found out, that method of raising errors is now 
frowned upon (deprecated). The way you are supposed to raise errors is to 
create a new class, subclassing Exception. Then you would be able to catch 
it by variable name as you are trying to do with the import. But you 
certainly can't import a constant string! It's like trying to import the 
contents of a string variable, instead of the variable itself. Again, the 
problem is deprecation, the rwhois will eventually have to be re-written so 
that it uses exception classes, instead of just raising string errors.

JS 



More information about the Tutor mailing list