我使用openCV的cv2来实现这个,我对它产生的错误不满意。所以我想捕捉错误,然后提出一个更合适的错误,这样程序员就可以更好地了解出了什么问题:
try:
cv2.imwrite(out_fn, img_arr, [jpg_q, 100, png_c, 9])
except:
raise UnsupportedFileFormatError(out_fn)
但是,这会引发两个例外:
Traceback (most recent call last):
File "/Users/admin/Documents/Studie/IN3110/assignment4/Blur/blur/funcs/blur.py", line 25, in blur_image
cv2.imwrite(out_fn, img_arr, [jpg_q, 100, png_c, 9])
cv2.error: OpenCV(4.1.1) /Users/travis/build/skvark/opencv-python/opencv/modules/imgcodecs/src/loadsave.cpp:662: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'imwrite_'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/admin/Documents/Studie/IN3110/assignment4/Blur/blur/funcs/blur.py", line 27, in blur_image
raise UnsupportedFileFormatError(out_fn)
blur.errors.errors.UnsupportedFileFormatError: Unsupported file ending: "kyk.jp"
我想做一些类似的事情:
if cv2.error as e:
e.ignoreError()
throw new appropriateError()
这是我用一种完全虚构的伪编程语言来说明问题的方法,但你明白我的意思了。我该怎么做呢?谢谢:))
两件事
首先。如果要使用异常,请从中引发另一个异常
第二不要盲目隐藏异常。始终要具体,并且确保您正在使用的异常只在您预期的时候发生
相关问题 更多 >
编程相关推荐