Roshan Book

My Tech Notebook

Python tutorial 10- Exception handling


Example

def main():
    try:
        fh=open(line.txt)
    except:
        print(‘could not open the file’)

     else:

           for line in fh: print(line.strip())
        
main()

For particular error

def main():
    try:
        fh=open(line.txt)
    except IOError:
        print(‘could not open the file’)

     else:

           for line in fh: print(line.strip())
        
main()

Getting error message

def main():
    try:
        fh=open(line.txt)
    except IOError as e:
        print(‘could not open the file’),e

     else:

           for line in fh: print(line.strip())
        
main()

We can also remove else statement and put the command directly into try. In this way execution stops once error occurs.

 

# Raising own exception

def main():

for line in readfile(‘x.lines.txt’)

: print(line.strip())

except IOError as e:

print(‘cannot read file:’,e)

except valueerror as e:

print(‘bad filename’,e)

def readfile(filename):

if filename.endswith(‘.txt’)

           fh=open(filename)

           return fh.readlines()

else:raise ValueError(‘Filename must end with .txt’)

main()

5 responses to “Python tutorial 10- Exception handling

  1. party October 26, 2011 at 7:12 am

    Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  2. SuecyErer November 4, 2011 at 3:48 am

    Just one more great way to save lots of income upon auto tires is always to employ payless coupon codes. You can choose a lot of discount coupons regarding different elements. You’ll find inexpensive exhaust coupon codes and affordable auto deals while successfully which can be employed for car repairs. A price reduction car voucher might be incredibly handy if you’re holding serp issues. Coupons are a great way in order to maintain bucks high are a variety of discount codes easily available on the market. The highest approach to discover a discount that you can use is to appear and feel on their behalf in the magazine. You may also learn coupon codes for the on the web. It is possible to track down discount codes on-line or perhaps get very simple computer discount coupons from a site. Payless coupon

  3. document destruction November 10, 2011 at 6:32 am

    Our motto is once you make a mistake, do it twice.

  4. Branche Katalog December 2, 2011 at 9:22 am

    Problems are not stop signs, they are guidelines.

  5. Gucci Handbags Outlet December 5, 2011 at 6:36 am

    I really like your blog and i respect your work. I’ll be a frequent visitor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: