Exceptions

All exceptions listed here are subclasses of exception.HostnameException

Overview and examples

e = "No exception so far"
try:
    hostname.Hostname("a.bad-.example")
except hostname.exception.HostnameException:
    e = "Caught some hostname expection"
except Exception:
    e = "Some other exception"

print(e)
Caught some hostname expection

If a candidate hostname does not meet the requirements of a domain name, we raise an hostname.exception.DomainNameException which wraps Exceptions from the dns package.

e = "No exception so far"
try:
    hostname.Hostname("a..example")
except hostname.exception.DomainNameException:
    e = "Caught some DNS expection"
except Exception:
    e = "Some other exception"

print(e)
Caught some DNS expection

Because we want to distinguish the case when something isn’t a hostname from when arguments are provided incorrectly, we use hostname.exception.NotAStringError instead of TypeError when the candidate hostname is not a string.

e = "No exception so far"
try:
    hostname.Hostname(1)
except  hostname.exception.NotAStringError:
    e = "Caught Hostname NotAStringError"
except TypeError:
    e = "Caught TypeError"
except Exception:
    e = "Some other expection"

print(e)
Caught Hostname NotAStringError

This allows us to use TypeError to check other arguments

e = "No exception so far"
try:
    hostname.Hostname("foo", unknown_flag = "bar")
except  hostname.exception.NotAStringError:
    e = "Caught Hostname NotAStringError"
except TypeError:
    e = "Caught TypeError"
except Exception:
    e = "Some other expection"

print(e)
Caught TypeError

If a candidate hostname triggers an error from the IDNA package, we wrap that in hostname.exception.IDNAException

Warning

Only one exception will be raised even if the candidate is invalid for multiple reasons. Which of the errors gets reported in such cases is undefined behavior.

All HostnameException classes

exception hostname.exception.BadCharacterError(*args, **kwargs) None

A forbidden character was found in a label

exception hostname.exception.BadHyphenError(*args, **kwargs) None

A hyphen is used at the beginning or end of a label

exception hostname.exception.DigitOnlyError(*args, **kwargs) None

The rightmost label contains only digits

exception hostname.exception.DomainNameException(*args, **kwargs) None

DNS Parsing raised an exception

exception hostname.exception.HostnameException(*args, **kwargs) None

A generic exception abstraction

exception hostname.exception.INDAException(*args, **kwargs) None

IDNA processing raised an exception.

exception hostname.exception.NoLabelError(*args, **kwargs) None

Hostnames must have at least 1 label

exception hostname.exception.NotASCIIError(*args, **kwargs) None

Non-ASCII when ALLOW_IDNA is not set

exception hostname.exception.NotAStringError(*args, **kwargs) None

Must be a string

exception hostname.exception.UnderscoreError(*args, **kwargs) None

An underscore appeared in a label it shouldn’t have.