SlideShare a Scribd company logo
Firoze Abdur Rakib
 Syntax errors, also known as parsing errors, are
perhaps the most common kind of error you
encounter while you are still learning Python.
 The parser repeats the offending line and displays a
little ‘arrow’ pointing at the earliest point in the line
where the error was detected. The error is detected
at the token preceding the arrow. File name and line
number are printed so you know where to look in
case the input came from a script.
 Even if a statement or expression is syntactically
correct, it may cause an error when an attempt is
made to execute it. Errors detected during
execution are called exceptions and are not
unconditionally fatal. Most exceptions are not
handled by programs, however, and result in
error messages like “cannot divide by zero” or
“cannot concatenate ‘str’ and ‘int’ objects”.
 It is possible to write programs that handle selected
exceptions. Consider the following, where a user-
generated interruption is signaled by raising the
KeyboardInterrupt exception.
 First the 'try' clause is executed until an exception
occurs, in which case the rest of 'try' clause is skipped
and the 'except' clause is executed (depending on type
of exception), and execution continues. If an exception
occurs which does not match the exception named in
the except clause, it is passed on to outer try
statements; if no handler is found, it is an unhandled
exception and execution stops.
 The last except clause (when many are declared)
may omit the exception name(s), to serve as a
wildcard. This makes it very easy to mask a real
programming error. It can also be used to print
an error message and then re-raise the
exception.
 The try-except statement has an optional else
clause, which, when present, must follow all
except clauses. It is useful for code that must be
executed if the try clause does not raise an
exception.
 The raise statement allows the programmer to
force a specified exception to occur.
 The sole argument to raise indicates the
exception to be raised.
 A simpler form of the raise statement allows one
to re-raise the exception (if you don’t want to
handle it):
 Programs may name their own exceptions by
creating a new exception class. These are derived
from the Exception class, either directly or
indirectly.
 Here, the def__init__() of Exception has been
overridden. The new behavior simply creates the
value attribute.
 The try statement has another optional clause
which is intended to define clean-up actions that
must be executed under all circumstances.
 A finally clause is executed before leaving the try
statement, whether an exception has occurred or
not. When an exception has occurred in the try
clause and has not been handled by an except
clause, it is re-raised after the finally clause has
been executed. The finally clause is also executed
“on the way out” when any other clause of the try
statement is exited using break/continue/return.
 Some objects define standard clean-up actions to
be undertaken when the object is no longer
needed, regardless of whether or not the
operation using the object succeeded or failed.
 The problem with this code is that it leaves the
file open for an indefinite amount of time after
the code has finished executing.
 The ‘with’ statement allows objects like files to be
used in a way that ensures they are always
cleaned up promptly and correctly.
Questions or concerns?
 Python v2.7 Documentation
Section 8
Link: http://docs.python.org/tutorial/errors.html
Ad

Recommended

Error and exception in python
Error and exception in python
junnubabu
 
Exception handlingpdf
Exception handlingpdf
gandra jeeshitha
 
Exception Handling in Python Programming.pptx
Exception Handling in Python Programming.pptx
vinayagrawal71
 
Exception Handling
Exception Handling
baabtra.com - No. 1 supplier of quality freshers
 
Unit 4-Exception Handling in Python.pdf
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
lecs101.pdfgggggggggggggggggggddddddddddddb
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
Exception Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
Exception handling in python
Exception handling in python
Lifna C.S
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
Exception Handling in python programming.pptx
Exception Handling in python programming.pptx
shririshsri
 
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
Python Lecture 7
Python Lecture 7
Inzamam Baig
 
Python Exception Handling
Python Exception Handling
Megha V
 
Python Exceptions Powerpoint Presentation
Python Exceptions Powerpoint Presentation
mitchellblack733
 
Exception handling.pptx
Exception handling.pptx
NISHASOMSCS113
 
Exception handling with python class 12.pptx
Exception handling with python class 12.pptx
PreeTVithule1
 
Python programming : Exceptions
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
Exception
Exception
Navaneethan Naveen
 
Exception Handling.pptx
Exception Handling.pptx
Pavan326406
 
Exception Handling using Python Libraries
Exception Handling using Python Libraries
mmvrm
 
Exception handling in python and how to handle it
Exception handling in python and how to handle it
s6901412
 
Chapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Exception handling in python
Exception handling in python
baabtra.com - No. 1 supplier of quality freshers
 
Exception handling.pptxnn h
Exception handling.pptxnn h
sabarivelan111007
 
Download Wondershare Filmora Crack Latest 2025
Download Wondershare Filmora Crack Latest 2025
hyderik195
 
Adobe Photoshop 2025 Cracked Latest Version
Adobe Photoshop 2025 Cracked Latest Version
hyderik195
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 

More Related Content

Similar to Firoze_Errors_Exceptions in python__.ppt (20)

Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
Exception Handling in python programming.pptx
Exception Handling in python programming.pptx
shririshsri
 
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
Python Lecture 7
Python Lecture 7
Inzamam Baig
 
Python Exception Handling
Python Exception Handling
Megha V
 
Python Exceptions Powerpoint Presentation
Python Exceptions Powerpoint Presentation
mitchellblack733
 
Exception handling.pptx
Exception handling.pptx
NISHASOMSCS113
 
Exception handling with python class 12.pptx
Exception handling with python class 12.pptx
PreeTVithule1
 
Python programming : Exceptions
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
Exception
Exception
Navaneethan Naveen
 
Exception Handling.pptx
Exception Handling.pptx
Pavan326406
 
Exception Handling using Python Libraries
Exception Handling using Python Libraries
mmvrm
 
Exception handling in python and how to handle it
Exception handling in python and how to handle it
s6901412
 
Chapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Exception handling in python
Exception handling in python
baabtra.com - No. 1 supplier of quality freshers
 
Exception handling.pptxnn h
Exception handling.pptxnn h
sabarivelan111007
 
Download Wondershare Filmora Crack Latest 2025
Download Wondershare Filmora Crack Latest 2025
hyderik195
 
Adobe Photoshop 2025 Cracked Latest Version
Adobe Photoshop 2025 Cracked Latest Version
hyderik195
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
 
Exception Handling in python programming.pptx
Exception Handling in python programming.pptx
shririshsri
 
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
Python Exception Handling
Python Exception Handling
Megha V
 
Python Exceptions Powerpoint Presentation
Python Exceptions Powerpoint Presentation
mitchellblack733
 
Exception handling.pptx
Exception handling.pptx
NISHASOMSCS113
 
Exception handling with python class 12.pptx
Exception handling with python class 12.pptx
PreeTVithule1
 
Exception Handling.pptx
Exception Handling.pptx
Pavan326406
 
Exception Handling using Python Libraries
Exception Handling using Python Libraries
mmvrm
 
Exception handling in python and how to handle it
Exception handling in python and how to handle it
s6901412
 
Download Wondershare Filmora Crack Latest 2025
Download Wondershare Filmora Crack Latest 2025
hyderik195
 
Adobe Photoshop 2025 Cracked Latest Version
Adobe Photoshop 2025 Cracked Latest Version
hyderik195
 

Recently uploaded (20)

DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
輪読会資料_Miipher and Miipher2 .
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
輪読会資料_Miipher and Miipher2 .
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Structured Programming with C++ :: Kjell Backman
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
Ad

Firoze_Errors_Exceptions in python__.ppt

  • 2.  Syntax errors, also known as parsing errors, are perhaps the most common kind of error you encounter while you are still learning Python.  The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. The error is detected at the token preceding the arrow. File name and line number are printed so you know where to look in case the input came from a script.
  • 3.  Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal. Most exceptions are not handled by programs, however, and result in error messages like “cannot divide by zero” or “cannot concatenate ‘str’ and ‘int’ objects”.
  • 4.  It is possible to write programs that handle selected exceptions. Consider the following, where a user- generated interruption is signaled by raising the KeyboardInterrupt exception.  First the 'try' clause is executed until an exception occurs, in which case the rest of 'try' clause is skipped and the 'except' clause is executed (depending on type of exception), and execution continues. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops.
  • 5.  The last except clause (when many are declared) may omit the exception name(s), to serve as a wildcard. This makes it very easy to mask a real programming error. It can also be used to print an error message and then re-raise the exception.  The try-except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception.
  • 6.  The raise statement allows the programmer to force a specified exception to occur.  The sole argument to raise indicates the exception to be raised.  A simpler form of the raise statement allows one to re-raise the exception (if you don’t want to handle it):
  • 7.  Programs may name their own exceptions by creating a new exception class. These are derived from the Exception class, either directly or indirectly.  Here, the def__init__() of Exception has been overridden. The new behavior simply creates the value attribute.
  • 8.  The try statement has another optional clause which is intended to define clean-up actions that must be executed under all circumstances.  A finally clause is executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause, it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is exited using break/continue/return.
  • 9.  Some objects define standard clean-up actions to be undertaken when the object is no longer needed, regardless of whether or not the operation using the object succeeded or failed.  The problem with this code is that it leaves the file open for an indefinite amount of time after the code has finished executing.  The ‘with’ statement allows objects like files to be used in a way that ensures they are always cleaned up promptly and correctly.
  • 11.  Python v2.7 Documentation Section 8 Link: http://docs.python.org/tutorial/errors.html