try:
import someProprietaryModule
except ImportError:
raise ImportError('It appears that <someProprietaryModule> is not installed...')
Khi chạy, nếu someProprietaryModule không được cài đặt, người ta thấy:
(traceback data)
ImportError: unknown module: someProprietaryModule
During handling of the above exception, another exception occurred:
(traceback data)
ImportError: It appears that <someProprietaryModule> is not installed...
Có lẽ tôi không muốn "Trong quá trình xử lý của ngoại lệ ở trên ..." dây chuyền (và các dòng trên nó) xuất hiện. Tôi có thể làm điều này:
_moduleInstalled = True
try:
import someProprietaryModule
except ImportError:
_moduleInstalled = False
if not _moduleInstalled:
raise ImportError('It appears that <someProprietaryModule> is not installed...')
Nhưng điều đó giống như một chút hack. Tôi có thể làm gì khác?
này có thể giúp http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions- in-modern-python –