我正在编写一个工具,将命令发送到谷歌灯塔的CMD,并希望在URL无效时捕获错误。我将使用什么异常?
我当前正在尝试在输入无效的URL时在异常中捕获RuntimeError。
try:
os.system("lighthouse --quiet {} {} {} {} {} --output-path={}/{}.html ".format(DevEmuStr,throttlingVar,CacheStr,presetVar,url,reportlocation,filename))
except RuntimeError:
print("Please provide a proper URL")我得到的不是“请提供正确的URL”,而是:
Runtime error encountered: The URL you have provided appears to be invalid.
LHError: INVALID_URL
at lighthouse (C:\Users\sugar\AppData\Roaming\npm\node_modules\lighthouse\lighthouse-core\index.js:44:11)
at chromeP.then._ (C:\Users\sugar\AppData\Roaming\npm\node_modules\lighthouse\lighthouse-cli\run.js:182:12)
at process._tickCallback (internal/process/next_tick.js:68:7)灯塔只是继续下一个URL
还有没有我能捕捉到的另一个错误?
发布于 2018-12-21 23:34:18
感谢每个试图帮助我的人,我终于找到了一种方法来获得它。
通过添加以下内容:
lh_url_ok = os.system("lighthouse --quiet {} {} {} {} {} --output-path={}/{}.html ".format(DevEmuStr,throttlingVar,CacheStr,presetVar,url,reportlocation,filename))
if lh_url_ok >0:
print("Error")我能够检查退出代码是否大于0 (0=no错误)
发布于 2018-12-21 21:39:52
不,你不能从Python中捕捉到异常。
在我看来,“遇到运行时错误”是灯塔输出的输出,它不是你可以捕捉到的真正的Python异常。
Python不知道从os.system开始的可执行文件内部发生了什么,您可以只获得输出和退出代码。
https://stackoverflow.com/questions/53885503
复制相似问题