Exceptions

220 views

Published on

Exception handling

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
220
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Exceptions

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. Exception• An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the programs instructions.• Handling an exception: • Defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
  3. 3. Syntax:Exception handlingtry: operationsexcept ExceptionI: If there is ExceptionI, then execute this block.except ExceptionII:If there is ExceptionII, then execute this block.else: If there is no exception then execute this block
  4. 4. Exampletry:fh = open("testfile", "w")fh.write("This is my test file for exception handling!!")except IOError:print "Error: cant find file or read data"else:print "Written content in the file successfully"fh.close()
  5. 5. User-Defined Exceptions:class Networkerror(RuntimeError):def __init__(self, arg): self.args = argtry:raise Networkerror("Bad hostname") except Networkerror,e: print e.args
  6. 6. 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
  7. 7. Contact Us
  8. 8. Contact Us

×