gnewsense-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gnewsense-dev] Debderive


From: Sam Geeraerts
Subject: Re: [Gnewsense-dev] Debderive
Date: Sat, 18 Feb 2012 13:05:39 +0100
User-agent: Thunderbird 2.0.0.24 (X11/20101029)

Stayvoid wrote:
Indeed. Every exception should be caught, so raising one in the main code is 
not good practice.
Could you tell a little bit more?
(I haven't read the chapter about the exceptions yet.)

Normally, you handle exceptions in this way:

fname = 'file.txt'
try:
  f = open(fname, 'r')
except:
  # Catching all exceptions.
  print >> sys.stderr, 'Unable to open %s.' % fname

When you raise an exception yourself, it should be caught further on so you can handle it elegantly. If you don't then it gets handled by the default exception handler, which just blurts out the execution stack.

def foo(bar):
  if bar < 0:
    raise Exception('FAIL!')

try:
  foo(1)
except:
  print >> sys.stderr, 'Something failed.'



reply via email to

[Prev in Thread] Current Thread [Next in Thread]