通常、インポートした場合socket
、例外を簡単にキャッチできます。
>>> import socket
>>> try:
... socket.gethostbyname('hello')
... except socket.gaierror:
... print('oops')
...
oops
しかし、インポートするだけsocket.gethostbyname
では機能しません。
>>> from socket import gethostbyname
>>> try:
... gethostbyname('hello')
... except socket.gaierror:
... print('oops')
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name 'socket' is not defined
NameError
キャッチしようとするともらえますgaierror
。
これに対する回避策はありますか?文字列(例)で例外をキャッチすることはできませんexcept 'socket.gaierror':
か?