Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocume...
Title of the presentation           Reshmi R           reshmir@ymail.com           www.facebook.com/reshmi.             ra...
Handling Exceptions• Exception: An error that occurs during the execution of a  program• Exception is raised and can be ca...
Try/Except try:  num = float(raw_input("Enter a number: "))except:  print "Something went wrong!"• ‘try’ statement sectio...
Try/Finally• In try/finally, finally block is always run whether an exception  occurs or not try:   <block of statements>...
Specifying an Exception Type try:  num = float(raw_input("nEnter a number: "))except(ValueError):  print "That was not a ...
Handling Multiple Exception Types              (contd…) for value in (None, "Hi!"): try:   print "Attempting to convert",...
Try/Else try:  num = float(raw_input("nEnter a number: "))except(ValueError):  print "That was not a number!"else:  print...
If this presentation helped you, please visit our           page facebook.com/baabtra and like it.               Thanks in...
Contact Us
Exception handling
Upcoming SlideShare
Loading in …5
×

Exception handling

224 views
195 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
224
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

Exception handling

  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. Title of the presentation Reshmi R reshmir@ymail.com www.facebook.com/reshmi. ramaswami twitter.com/reshmir1 in.linkedin.com/in/reshmi r +917736719717
  3. 3. Handling Exceptions• Exception: An error that occurs during the execution of a program• Exception is raised and can be caught (or trapped) then handled• Unhandled, an exception halts the program and an error message is displayed>>> 1/0Traceback (most recent call last): File "<pyshell#0>", line 1, in -toplevel- 1/0ZeroDivisionError: integer division or modulo by zero
  4. 4. Try/Except try: num = float(raw_input("Enter a number: "))except: print "Something went wrong!"• ‘try’ statement sections of code that could raise exception• If an exception is raised, then the ‘except’ block is run• If no exception is raised, then the ‘except’ block is skipped
  5. 5. Try/Finally• In try/finally, finally block is always run whether an exception occurs or not try: <block of statements>finally: <block of statements>• Ensure some actions to be done in any case• It can not be used in the try with except and else.
  6. 6. Specifying an Exception Type try: num = float(raw_input("nEnter a number: "))except(ValueError): print "That was not a number!“• Different types of errors raise different types of exceptions• except clause can specify exception types to handle• Attempt to convert "Hi!" to float raises ValueError exception• Avoid general, catch-all exception handling• Specify exception types to handle each individual case
  7. 7. Handling Multiple Exception Types (contd…) for value in (None, "Hi!"): try: print "Attempting to convert", value, "–>", print float(value) except(TypeError): print "Can only convert string or number!" except(ValueError): print "Can only convert a string of digits!“• Each except clause can offer specific code for each individual exception type
  8. 8. Try/Else try: num = float(raw_input("nEnter a number: "))except(ValueError): print "That was not a number!"else: print "You entered the number", num• Can add single else clause after all except clauses• else block executes only if no exception is raised• num printed only if assignment statement in the try block raises no exception
  9. 9. 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
  10. 10. Contact Us

×