On Wed, Aug 14, 2019 at 01:19:05PM -0000, Marat Sharafutdinov wrote:

> While using any library there is a need to handle exceptions it 
> raises.

Is that true? For most exceptions, an exceptions means there is a bug in 
your code. The exception is a sign that you are doing something wrong, 
and have to fix your code, not something for you to catch and handle.


> The essence of my idea is simple and does not affects compatibility 
> with existing code: to automatically define a magic attribute of 
> module under the working name "__exception__" while importing any 
> module. It can be used in usual way, for example:
>
> >>> try:
> ...     any_module.anything
> ... except any_module.__ exception__:
> ...     ...

Is __exception__ generated automatically? How is the interpreter 
supposed to know what exceptions can be raised by the library, and which 
ones should be caught by calling code?

Why do you think that a *single* except block for *everything* in the 
module is sufficient?


> Here any exception an author of library raises can be handled. This 
> way you can handle any exception that occurs in the library without 
> even knowing what exceptions it can raise.

How can you handle an exception without knowing what sort of exception 
it is and what it means?

It is easy to write toy pseudo-code where the exception handling is 
"..." but it is another story about writing real code that catches real 
exceptions.

Let's take pathlib as an example. It can raise (amoung other exceptions) 
KeyError, NotImplementedError, TypeError, FileExistsError, and 
FileNotFoundError.

What sort of meaningful error handler can you write that will treat all 
of those exceptions, and all the others I haven't listed, from the same 
except block, without knowing which exception actually occurred?



-- 
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/YJNYR7FGCVEJZATW2XPMZDYRFXGZZKOW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to