Is it ok for PyPDF2 to throw non-PyPDF2 exceptions? #1210
Replies: 2 comments 4 replies
-
My understanding of exceptions is that they should be (at least to some degree) predictable for the user. For instance, when I pass a dict with some mandatory fields to a function, it's okay for the function to raise On the other hand, when I pass an invalid PDF file to PyPDF2, the story is different. PyPDF2 creates some dict internally (the user has zero control over this) and when it tries to access it internally, it fails with My comments to your questions:
Yes. When I see a
No, |
Beta Was this translation helpful? Give feedback.
-
Looking at the examples / issues / discussions so far, I have a preference for custom pypdf exceptions. Of course, the stacktrace should still show where exactly the exception was thrown so that we could capture pypdf bugs / investigate what exactly is broken for robustness fixes. Give this a 👍 if you agree and 👎 if you think we should keep the default exceptions. We already have:
|
Beta Was this translation helpful? Give feedback.
-
Some PDF documents are so severely broken, that we cannot fix them. And issues can occur at many places.
Assume there is an issue which we cannot fix. We have to raise an exception. Is it ok to raise non-PyPDF2 exceptions like a ValueError when
int(foo)
fails?How do other non-core libraries handle it?
Similar discussions
This topic was essentially discussed in #584, but the focus was not clearly on that question. It popped up again in #1279.
Other issues:
Our options
Allow non-PyPDF2 exceptions (currently the case)
Pro:
Only have PyPDF2 exceptions
If we want to throw e.g.
PyPDF2.errors.PdfReadError
for any potential issue that might happen, we might need pretty big try-except blocks internally.Pro:
Con:
Open questions
PdfReadError
and not e.g.ValueError
or similar?Comparisons
Some comparisons to other popular Python libraries:
JSON: Custom exceptions
PyYAML: Standard python exceptions
Zipfile: Custom exceptions
Beta Was this translation helpful? Give feedback.
All reactions