SlideShare uma empresa Scribd logo
1 de 11
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
Title of the presentation


           Reshmi R
           reshmir@ymail.com
           www.facebook.com/reshmi.
             ramaswami
           twitter.com/reshmir1
           in.linkedin.com/in/reshmi r
           +917736719717
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/0
Traceback (most recent call last):
 File "<pyshell#0>", line 1, in -toplevel-
  1/0
ZeroDivisionError: integer division or modulo by zero
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
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.
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
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
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
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
Contact Us

Mais conteúdo relacionado

Destaque (6)

It careers
It careersIt careers
It careers
 
Xml
XmlXml
Xml
 
Project management difference between industry and college
Project management difference between industry and collegeProject management difference between industry and college
Project management difference between industry and college
 
Complex number
Complex numberComplex number
Complex number
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 

Semelhante a Exception handling

Exception Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptException Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptRaja Ram Dutta
 
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Wen-Tien Chang
 
Exception handling and function in python
Exception handling and function in pythonException handling and function in python
Exception handling and function in pythonTMARAGATHAM
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyWen-Tien Chang
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingP3 InfoTech Solutions Pvt. Ltd.
 
exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptxSulekhJangra
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapriyankazope
 
Python exceptions
Python exceptionsPython exceptions
Python exceptionsrikbyte
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerByte
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingAboMohammad10
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.pptAjit Mali
 

Semelhante a Exception handling (20)

Exception Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptException Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.ppt
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)
 
Exception handling and function in python
Exception handling and function in pythonException handling and function in python
Exception handling and function in python
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in Ruby
 
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception HandlingPython Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M21 - Exception Handling
 
exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Python exceptions
Python exceptionsPython exceptions
Python exceptions
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim Muller
 
Py-Slides-9.ppt
Py-Slides-9.pptPy-Slides-9.ppt
Py-Slides-9.ppt
 
Exception
ExceptionException
Exception
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Python Lecture 7
Python Lecture 7Python Lecture 7
Python Lecture 7
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
41c
41c41c
41c
 
ACP - Week - 9.pptx
ACP - Week - 9.pptxACP - Week - 9.pptx
ACP - Week - 9.pptx
 

Mais de baabtra.com - No. 1 supplier of quality freshers

Mais de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 

Exception handling

  • 1.
  • 2. 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
  • 3. Title of the presentation Reshmi R reshmir@ymail.com www.facebook.com/reshmi. ramaswami twitter.com/reshmir1 in.linkedin.com/in/reshmi r +917736719717
  • 4. 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/0 Traceback (most recent call last): File "<pyshell#0>", line 1, in -toplevel- 1/0 ZeroDivisionError: integer division or modulo by zero
  • 5. 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
  • 6. 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.
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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