Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
EXCEPTIONS IN PYTHON

SHAMEENA.V
shameenavayoli@gmail.com
www.facebook.com/shamee
na latheef
twitter.com/shameena
in.linke...
Introduction :
Python uses exceptions to communicate
errors and anomalies
 exception is an object that indicates an erro...
The try Statement
• The try statement provides Python's
exception-handling mechanism
• It is a compound statement
• It can...
1)try/except
Syntax:
try:
statement(s)
except [expression [, target]]:
statement(s)
[else:
statement(s)]
Contd….
• The body of each except clause is known as an
exception handler
2) try/finally
Syntax:
try:
statement(s)
finally:
statement(s)
The finally clause establishes what is known
as a clean-up ...
Example :
f = open(someFile, "w")
try:
do_something_with_file(f)
finally:
f.close( )
Built-In Exception classes
• BaseException
mother of all exceptions
• Exception :
StandardError(base class for all built i...
AttributeError (On attribute reference or assignment failure )
EnvironmentError (On error outside Python)
IOError
OSError
...
NameError
UnBoundLocalError
On reference to an unassigned local variable

SyntaxError
On parser encountering a syntax erro...
Example:
try:
con = psycopg2.connect(database='testdb', user='shafeequemonp' )
cur = con.cursor()
print "Connecting to dat...
THANKS
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.

Thanks in advance.
www.baabtra.c...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Start up Vil...
Exceptions in python
Upcoming SlideShare
Loading in...5
×

Exceptions in python

494

Published on

Exceptions in python

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
494
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Exceptions in python

  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. EXCEPTIONS IN PYTHON SHAMEENA.V shameenavayoli@gmail.com www.facebook.com/shamee na latheef twitter.com/shameena in.linkedin.com/in/shamean 9567135569
  3. 3. Introduction : Python uses exceptions to communicate errors and anomalies  exception is an object that indicates an error or anomalous condition code explicitly raise an exception by executing a raise statement.  If a program does not handle an exception, it terminates with an error traceback message
  4. 4. The try Statement • The try statement provides Python's exception-handling mechanism • It is a compound statement • It can take one of two different forms: 1) try clause followed by one or more except clauses 2) A try clause followed by exactly one finally clause
  5. 5. 1)try/except Syntax: try: statement(s) except [expression [, target]]: statement(s) [else: statement(s)]
  6. 6. Contd…. • The body of each except clause is known as an exception handler
  7. 7. 2) try/finally Syntax: try: statement(s) finally: statement(s) The finally clause establishes what is known as a clean-up handler
  8. 8. Example : f = open(someFile, "w") try: do_something_with_file(f) finally: f.close( )
  9. 9. Built-In Exception classes • BaseException mother of all exceptions • Exception : StandardError(base class for all built in exception) ArithmeticError FloatingPointError OverflowError ZeroDivisionError
  10. 10. AttributeError (On attribute reference or assignment failure ) EnvironmentError (On error outside Python) IOError OSError WindowsError ImportError (On failure of import to find module or name.) LookupError base class for IndexError, KeyError – IndexError On out-of-range sequence subscript - KeyError On reference to a non-existent mapping (dict) key
  11. 11. NameError UnBoundLocalError On reference to an unassigned local variable SyntaxError On parser encountering a syntax error – IndentationError On parser encountering an indentation syntax error --TabError On improper mixture of spaces and tabs
  12. 12. Example: try: con = psycopg2.connect(database='testdb', user='shafeequemonp' ) cur = con.cursor() print "Connecting to databasen ->%s" % (con) cur.execute("""INSERT INTO tbl_user(vchr_username) VALUES(%s)""",(ins_login_base.str_name)) con.commit() except psycopg2.DatabaseError, e: if con: con.rollback() print 'Error %s' % e sys.exit(1) finally: if con: con.close() return
  13. 13. THANKS
  14. 14. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  15. 15. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×