exceptions in python
DESCRIPTION
Exceptions in pythonTRANSCRIPT
![Page 1: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/1.jpg)
![Page 2: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/2.jpg)
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
![Page 3: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/3.jpg)
EXCEPTIONS IN PYTHON
[email protected]/shameena
latheeftwitter.com/shameenain.linkedin.com/in/shamean9567135569
![Page 4: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/4.jpg)
Introduction :Python uses exceptions to communicate
errors and anomalies exception is an object that indicates an error
or anomalous conditioncode explicitly raise an exception by executing
a raise statement. If a program does not handle an exception, it
terminates with an error traceback message
![Page 5: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/5.jpg)
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
![Page 6: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/6.jpg)
1)try/except
Syntax:try: statement(s)except [expression [, target]]: statement(s)[else: statement(s)]
![Page 7: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/7.jpg)
Contd….
• The body of each except clause is known as an exception handler
![Page 8: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/8.jpg)
2) try/finally
Syntax:try: statement(s)finally: statement(s)
The finally clause establishes what is known as a clean-up handler
![Page 9: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/9.jpg)
Example :f = open(someFile, "w")try: do_something_with_file(f)finally: f.close( )
![Page 10: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/10.jpg)
Built-In Exception classes
• BaseException mother of all exceptions• Exception : StandardError(base class for all built in
exception)ArithmeticError
FloatingPointError OverflowErrorZeroDivisionError
![Page 11: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/11.jpg)
AttributeError (On attribute reference or assignment failure )
EnvironmentError (On error outside Python)IOErrorOSErrorWindowsError
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
![Page 12: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/12.jpg)
NameErrorUnBoundLocalError 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
![Page 13: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/13.jpg)
Example:try: con = psycopg2.connect(database='testdb', user='shafeequemonp' ) cur = con.cursor() print "Connecting to database\n ->%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
![Page 14: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/14.jpg)
THANKS
![Page 15: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/15.jpg)
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
![Page 16: Exceptions in python](https://reader036.vdocument.in/reader036/viewer/2022082810/5588f7ded8b42a9e4e8b4668/html5/thumbnails/16.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]