Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylint warning on 'except Exception:'

Tags:

python

pylint

For a block like this:

try:
    #some stuff
except Exception:
    pass

pylint raises warning W0703 'Catch "Exception"'. Why?

like image 580
yanchenko Avatar asked Apr 16 '09 13:04

yanchenko


People also ask

How do I ignore Pylint warning?

This may be done by adding # pylint: disable=some-message,another-one at the desired block level or at the end of the desired line of code.

How do I use except exception as e in Python?

User-Defined Exceptions This is useful when you need to display more specific information when an exception is caught. In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to create an instance of the class Networkerror.

What is except exception as e?

In contrast, the except Exception as e statement is a statement that defines an argument to the except statement. e in the latter statement is utilized to create an instance of the given Exception in the code and makes all of the attributes of the given Exception object accessible to the user.31-Dec-2021.

How do I see exception messages in Python?

If you want the error class, error message, and stack trace, use sys. exc_info() . The function sys. exc_info() gives you details about the most recent exception.


4 Answers

It's considered good practice to not normally catch the root Exception object, instead of catching more specific ones - for example IOException.

Consider if an out of memory exception occurred - simply using "pass" isn't going to leave your programme in a good state.

Pretty much the only time you should catch Exception is at the top level of your programme, where you can (try to) log it, display an error, and exit as gracefully as you can.

like image 180
Greg Avatar answered Oct 20 '22 00:10

Greg


It's good practice to catch only a very narrow range of types. 'Exception' is too general - you will end up catching not just the errors you planned for, but other errors too, which may mask bugs in your code that would be quicker to diagnose if they weren't caught at all, or possibly would be better dealt with by a single very high level exception handler.

Having said that, since Python2.6, catching Exception has become a lot more reasonable, because all the exceptions that you wouldn't want to catch (SystemExit, KeyboardInterrupt) no longer inherit from Exception. They instead inherit from a common BaseException instead. This has been done deliberately in order to make catching Exception relatively harmless, since it is such a common idiom.

See PEP 3110 for details & future plans.

like image 40
2 revs Avatar answered Oct 19 '22 22:10

2 revs


because it thinks that you're catching too much. and it's right.

like image 43
SilentGhost Avatar answered Oct 19 '22 23:10

SilentGhost


Exception are raised when something... exceptional occurs. It's generally a good thing that the program terminates.

You may want to ignore some exceptions, but IMO there's no good reason for catching a base-class like that.

like image 23
Bastien Léonard Avatar answered Oct 19 '22 22:10

Bastien Léonard