Re: [Tutor] question about try except

2005-10-27 Thread Alan Gauld
 and i am using some try / except statements. 
 the problem is, that even though my script does not crash, i dont know
 the exact error. 

IT sounds like your try/except is masking the error.
Its usually a good idea to NOT Use try/except when developing your 
code, then go back and put it in when the basics are working. That way 
Pythons own exception handling will catch the error, force a break and 
print the stacktrace whoich helps identify the problem.

 is there a parameter that will allow me to use try and except but that
 will also pring out the traceback statements that python usually does to
 the terminal?

One technique is to simply call raise within each exception block, 
then comment out the raise lines when you no longer need them.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-27 Thread Kent Johnson
nephish wrote:
 Hey there,
   i am writing some (for me) pretty complicated stuff for work that
 really needs to work. 
   i have looked at exception handling in the Learning Python book.
 and i am using some try / except statements. 
   the problem is, that even though my script does not crash, i dont know
 the exact error. 
   is there a parameter that will allow me to use try and except but that
 will also pring out the traceback statements that python usually does to
 the terminal?

If you put traceback.print_exc() in your exception handler it will print out 
the exception string and the full stack trace. This is very handy when you have 
unexpected exceptions as it will let you know where the problem occurs.

For example:
try:
  open('foo.txt')
except IOError:
  import traceback
  traceback.print_exc()

Kent

-- 
http://www.kentsjohnson.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-27 Thread w chun
On 10/26/05, nephish [EMAIL PROTECTED] wrote:
 Yeah, cool. i am just starting this part.
 i am glad i started with python.
 thanks for the help


if your app is willing to tolerate errors/crashes, then i would take
alan's advice and just letting the errors happen, as opposed to being
so careful with (and integrating Kent's suggestion for the full
traceback with):

try:
BLOCK
except Exception, e:
print e, traceback.print_exc()
# take some other evasive yet safe maneuver

i only use the above when integrating into large systems (that aren't
supposed to crash, that need some cleanup, and/or that need to take
other action).  otherwise for simple scripts, i just let the darn
thing crash out and add in the handler for those errors.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2006,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-27 Thread Kent Johnson
w chun wrote:
 if your app is willing to tolerate errors/crashes, then i would take
 alan's advice and just letting the errors happen, as opposed to being
 so careful with (and integrating Kent's suggestion for the full
 traceback with):
 
 try:
 BLOCK
 except Exception, e:
 print e, traceback.print_exc()
 # take some other evasive yet safe maneuver

No, not quite. print_exc() does its own output and returns None, so it 
shouldn't be part of the print statement. And print_exc() prints e as part of 
its output, so 'print e' is redundant. All you need in the except block is 
traceback.print_exc() and whatever additional handling you want to do.

Normally it's not a good idea to catch generic Exception - you usually can be 
more focused than that - but at a high level in an application it can be useful 
to catch everything. For example you may be processing a list of files in a 
loop and you don't want an error in one file to abort the loop. So you put a 
try / except at the top level of the loop to catch and log errors, then 
continue with the next file.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-26 Thread Hugo González Monteverde
Yes,

You can catch an error object along with the exception, as in:

try:
 fileo = open(nofile)
except IOError, e:
 print Alas..., e

As you see, the error object has a string representation equal wo what 
normally the python interpreter prints...

 
Alas... [Errno 2] No such file or directory: 'nofile'
 


Hope it helps. It took me originally a long time to know this trick, as 
it's kinda buried in the docs.

Hugo


nephish wrote:
 Hey there,
   i am writing some (for me) pretty complicated stuff for work that
 really needs to work. 
   i have looked at exception handling in the Learning Python book.
 and i am using some try / except statements. 
   the problem is, that even though my script does not crash, i dont know
 the exact error. 
   is there a parameter that will allow me to use try and except but that
 will also pring out the traceback statements that python usually does to
 the terminal?
 
   thanks
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-26 Thread nephish
Thanks Hugo,
Now that i know where to look
appreciate your help.
-sk


On Wed, 2005-10-26 at 21:27 -0600, Hugo González Monteverde wrote:
 Yes,
 
 You can catch an error object along with the exception, as in:
 
 try:
  fileo = open(nofile)
 except IOError, e:
  print Alas..., e
 
 As you see, the error object has a string representation equal wo what 
 normally the python interpreter prints...
 
  
 Alas... [Errno 2] No such file or directory: 'nofile'
  
 
 
 Hope it helps. It took me originally a long time to know this trick, as 
 it's kinda buried in the docs.
 
 Hugo
 
 
 nephish wrote:
  Hey there,
  i am writing some (for me) pretty complicated stuff for work that
  really needs to work. 
  i have looked at exception handling in the Learning Python book.
  and i am using some try / except statements. 
  the problem is, that even though my script does not crash, i dont know
  the exact error. 
  is there a parameter that will allow me to use try and except but that
  will also pring out the traceback statements that python usually does to
  the terminal?
  
  thanks
  
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
  

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-26 Thread w chun
 i am writing some (for me) pretty complicated stuff for work that
 really needs to work.
 i have looked at exception handling...
 and i am using some try / except statements.
 the problem is, that even though my script does not crash, i dont know
 the exact error.
 is there a parameter that will allow me to use try and except but that
 will also pring out the traceback statements that python usually does to
 the terminal?


exception handling is one of the greatest strengths of Python and
other high-level languages with this feature.  it allows the
programmer to anticipate potential problems and perhaps be able to
accept and process them at runtime.

let's say you have a code block called BLOCK.  newbies to Python would
typically do something like this to ensure that errors don't happen:

try:
BLOCK
except:
pass

however, this is not the case.  if errors *do* happen, they are thrown
away, thus serves no one any good, not the programmer nor the user.

the best solution is to catch specific exceptions and handle each
case.  (sure, and having just one handler for multiple exceptions is
also okay.).  one example is hugo's where he catches an IOError
exception and uses the exception instance 'e' to get more info out of
it.

now if you *don't* know what exceptions may happen, you can do
something similar.  it's almost a combination of the above two
handlers:

try:
BLOCK
except Exception, e:
print 'Caught exception without a specific handler:, e

this will at least tell you what exception happens in BLOCK, so that
you can modify it to be something like:

try:
BLOCK
except YourSpecificException, e:
# handle YourSpecificException code
except Exception, e:
print 'Caught exception without a specfic handler:', e

once you know the range of exceptions that may happen in BLOCK and
have written handlers for them, you can dispense with the general
catch-all.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2006,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about try except

2005-10-26 Thread nephish
Yeah, cool. i am just starting this part.
i am glad i started with python.
thanks for the help
sk


On Wed, 2005-10-26 at 21:32 -0700, w chun wrote:
  i am writing some (for me) pretty complicated stuff for work that
  really needs to work.
  i have looked at exception handling...
  and i am using some try / except statements.
  the problem is, that even though my script does not crash, i dont 
  know
  the exact error.
  is there a parameter that will allow me to use try and except but 
  that
  will also pring out the traceback statements that python usually does to
  the terminal?
 
 
 exception handling is one of the greatest strengths of Python and
 other high-level languages with this feature.  it allows the
 programmer to anticipate potential problems and perhaps be able to
 accept and process them at runtime.
 
 let's say you have a code block called BLOCK.  newbies to Python would
 typically do something like this to ensure that errors don't happen:
 
 try:
 BLOCK
 except:
 pass
 
 however, this is not the case.  if errors *do* happen, they are thrown
 away, thus serves no one any good, not the programmer nor the user.
 
 the best solution is to catch specific exceptions and handle each
 case.  (sure, and having just one handler for multiple exceptions is
 also okay.).  one example is hugo's where he catches an IOError
 exception and uses the exception instance 'e' to get more info out of
 it.
 
 now if you *don't* know what exceptions may happen, you can do
 something similar.  it's almost a combination of the above two
 handlers:
 
 try:
 BLOCK
 except Exception, e:
 print 'Caught exception without a specific handler:, e
 
 this will at least tell you what exception happens in BLOCK, so that
 you can modify it to be something like:
 
 try:
 BLOCK
 except YourSpecificException, e:
 # handle YourSpecificException code
 except Exception, e:
 print 'Caught exception without a specfic handler:', e
 
 once you know the range of exceptions that may happen in BLOCK and
 have written handlers for them, you can dispense with the general
 catch-all.
 
 hope this helps!
 -- wesley
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Core Python Programming, Prentice Hall, (c)2006,2001
 http://corepython.com
 
 wesley.j.chun :: wescpy-at-gmail.com
 cyberweb.consulting : silicon valley, ca
 http://cyberwebconsulting.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor