exception problem

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 27 23:44:43 EDT 2012


On Wed, 27 Jun 2012 17:13:00 -0700, Charles Hixson wrote:

> On 06/25/2012 12:48 AM, Steven D'Aprano wrote:
>> On Sun, 24 Jun 2012 16:16:25 -0700, Charles Hixson wrote:
>>
>>
>>> But what I wanted was to catch any exception.
>>>      
>> Be careful of what you ask for, since you might get it.
>>
>> "Catch any exception" is almost certainly the wrong thing to do, almost
>> always. The one good reason I've seen for a bare except is to wrap the
>> top level of an application in order to redirect uncaught exceptions to
>> something other than the console (such as a GUI error dialog box). And
>> even then, you probably don't want to catch *all* exceptions, but only
>> those that inherit from Exception:
>>
>> try:
>>      main()  # run my application
>> except Exception as err:
>>      display_error_dialog(err)
>>      # or log to a file, or something...
>>
>>
>>
>>
> This time it was the right thing, as I suspected that *SOME* exception
> was being thrown, but had no idea what one.  The problem was I didn't
> know how to print the result when I caught the exception.
> [...] once I figured out how
> to list the result of catching the exception.

If you had simply NOT caught it, the full traceback would have been 
printed automatically and you could have easily seen what exception was 
being raised, the error message, and where it was being raised from.

This is exactly why you should not have used a bare except, or any except 
at all, but just let the uncaught exception print on its own. When trying 
to diagnose bugs, try...except is not your friend. Tracebacks are your 
friend.

[...]
> What really annoys me is the way the documentation has worsened since
> python 2.5, but if you know what it is trying to tell you, then I guess
> you aren't bothered by undefined terms and lack of examples.  I went
> away from programming in Python for a couple of years though, and I
> guess I missed the transition, or something.

If you have concrete examples of where you think the documentation could 
be improved, please either raise an enhancement request or bug report:

http://bugs.python.org/

or at least raise them here (and hope somebody else raised the bug 
report).

Otherwise, there's nothing we can do about vague complaints.



-- 
Steven



More information about the Python-list mailing list