Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Too Broad of an exception

88 views
Skip to first unread message

rsutton

unread,
Oct 25, 2023, 10:20:04 AM10/25/23
to
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.

Here's what I'm trying to do:
I am using robocopy to to copy a set of files to/from a LAN location and
my desktop(s). Here is the partial code:

p = subprocess.run(["robocopy.exe",
STORE_LOCATION, NEWS_LOCATION,
"/NFL", "/NDL", "/NJH", "/NJS", "/NP", "/XF",
"msgFilterRules.dat",
"xmsgFilterRules.dat"], check=False)

# this is necessary because Windows Robocopy returns
# various return codes for success.
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)

But the code I have written does exactly what I want. If the returncode
is 7 or less, then I have success. If the returncode is 8 or above
there is a failure and I want to see what the returncode is.

Trying to read the python Exception docs is mind bending. Any help
would be appreciated.

Richard

Rene Kita

unread,
Oct 25, 2023, 10:26:04 AM10/25/23
to
rsutton <rsut...@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.

rsutton

unread,
Oct 25, 2023, 11:49:32 AM10/25/23
to
On 10/25/2023 11:06 AM, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> outer quotation marks) prints some prominent exception types. After
>> manually removing those that do not seem to apply, I am left with:
>> "AssertionError",
>> "ChildProcessError",
> ...
>
> "Manually removing" above was meant to be a fast first pass,
> where I only excluded exception types that were obviously
> inappropriate. It is now to be followed by a search for the
> appropriate exception types among those exception types left.
>
>
@Rene & @Stefan,
I really appreciate the guidance provided. By replacing Exception with
RuntimeError, pylint seems happy! More specificity, I guess. I know
that I could have ignored the pylint exceptions, but I want to use this
as a learning experience. I looks like I have a lot of reading to do on
exception handling. IMO all of the try/except code looks quite clumsy to
me. It may be excellent for some tasks but to me, it looks quite
inelegant. Like I said, I have a lot to learn.

Thank you both for your guidance.

Richard

Kushal Kumaran

unread,
Oct 25, 2023, 1:49:04 PM10/25/23
to
>From what you've described of your problem, it seems like a small-ish
utility program you're writing for your own use. You don't need any
`try`...`except` blocks in such code. You just let the exception stop
your program.

--
regards,
kushal

Thomas Passin

unread,
Oct 25, 2023, 4:03:32 PM10/25/23
to
In this particular case you could probably just handle the return result
from robocopy right there. Of course, it depends on the rest of the
code. It probably doesn't really need an exception, and if you want to
handle it in the calling function, you could just return the result as a
boolean:

return p.returncode < 8 # Reversed the test since a return of "True"
# would make more sense as a return code

dn

unread,
Oct 25, 2023, 9:32:52 PM10/25/23
to
On 26/10/2023 04.49, rsutton via Python-list wrote:
> On 10/25/2023 11:06 AM, Stefan Ram wrote:
>> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>> outer quotation marks) prints some prominent exception types. After
...

>>    "Manually removing" above was meant to be a fast first pass,
>>    where I only excluded exception types that were obviously
>>    inappropriate. It is now to be followed by a search for the
>>    appropriate exception types among those exception types left.
>>
>>
> @Rene & @Stefan,
> I really appreciate the guidance provided.  By replacing Exception with
...


It would appear that (at least one message) did not make it to email -
neither to the list-archive.

People wishing to learn are unable to benefit a response, if it is not
sent to the list!

--
Regards,
=dn

Rene Kita

unread,
Oct 26, 2023, 5:04:50 AM10/26/23
to
^^^^^^^^^^^^^^^^

Ingrid says it's a RuntimeError, not RuntimeException.

Mats Wichmann

unread,
Oct 26, 2023, 1:10:54 PM10/26/23
to
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.

0 new messages