Too Broad of an exception

Mats Wichmann mats at wichmann.us
Thu Oct 26 13:10:20 EDT 2023


On 10/26/23 03:04, Rene Kita via Python-list wrote:
> Rene Kita <mail at rkta.de> wrote:
>> rsutton <rsutton43 at comcast.net> wrote:
>>> Hi all,
>>> I am fairly new to python (ie < 2 years).  I have a question about
>>> pylint.  I am running on windows 10/11, python 3.10.11.
>> [...]
>>>          if p.returncode >= 8:
>>>              raise Exception(f'Invalid result: {p.returncode}')
>>>
>>> It actually runs fine.  But pylint is not having it.  I get:
>>>
>>> win_get_put_tb_filters.py:61:12: W0719: Raising too general exception:
>>> Exception (broad-exception-raised)
>>
>> pylint is just a linter, ignore it if the code works and you like it the
>> way it is.
>>
>> pylint complains because you use Exception. Use e.g. RuntimeException to
>> silence it.
>                                                         ^^^^^^^^^^^^^^^^
> 
> Ingrid says it's a RuntimeError, not RuntimeException.

Meanwhile, the purpose of this complaint from pylint (and notice it's a 
"warning", not an "error", so take that for what it's worth), is that 
you usually want to convey some information when you raise an exception. 
Of course, you can put that into the message you pass to the class 
instance you raise, but the type of exception is informational too. 
Raising just "Exception" is equivalent to saying "my car is broken", 
without specifying that the starter turns but won't "catch", or starts 
but the transmission won't engage, or the battery is dead, or .... so 
it's *advising* (not forcing) you to be more informative.



More information about the Python-list mailing list