SlideShare a Scribd company logo
ADVANCE COMPUTER
PROGRAMMING
Week 9
By: Ammarah Khalid (Lecturer CS)
Riphah International University (Faisalabad)
Exceptions
• An exception is a condition which occurs during the
execution of a program that disrupts the normal flow of
the program's instructions
• In general, when a Python script encounters a situation
that it cannot cope with, it raises an exception
• Whenever an exception occurs, the program stops the execution,
and thus the further code is not executed
• An exception is a Python object that represents an
runtime error
Exceptions (Cont.)
Common Exceptions
• Python provides the number of built-in exceptions
• A list of common exceptions that can be thrown from a
standard Python:
• ZeroDivisionError: It occurs when a number is divided by zero
• NameError: It occurs when a name is not found. It may be local or
global
• ValueError: It occurs when there is an issue with the content of
the object to which you wanted to assign a value
• IOError: It occurs when Input Output operation fails
• EOFError: It occurs when the end of the file is reached, and yet
operations are being performed
If Exception Is Not Handled..
Exception Handling In Python
try/except
• To handle exceptions we need to use try and except block
• Define risky code that can raise an exception inside
the try block and corresponding handling code inside
the except block
try/except Syntax And Example
Using else With try/except
• Use the else statement with the try-except statement in
case to address the code which will be executed if no
exception occurs in the try block
Using else With try/except (Cont.)
Using else With try/except (Cont.)
Using else With try/except (Cont.)
• Exception variable can also be used with except
statement
• It is done by using the as keyword
• This object will return the cause of the exception
Catching Specific Exception
• A specific exception can also be caught
• It is good practice to specify an exact exception that the
except clause should catch
• Suppose the user enters the denominator as zero
• In that case, the try block will throw a ZeroDivisionError,
and we can catch that
Handling Multiple Exceptions
• A piece of code can throw several different exceptions
• It is needed to account for all of the potential exceptions
that could be raised within try block
• If the user enters a non-numeric value then, the try block
will throw a ValueError exception
Handling Multiple Exceptions (Cont.)
• If the user enters the denominator as zero, the try block
will throw a ZeroDivisionError
• Thus it can be specified which exception except block
should catch or handle
• A try block can be followed by multiple numbers
of except blocks to handle the different exceptions
• But only one exception will be executed when an
exception occurs
Handling Multiple Exceptions (Cont.)
• Multiple exceptions can also be handled with a
single except clause
• For that, use an tuple of values to specify multiple
exceptions in an except clause
try/finally
• Python provides the finally block, which is used with the
try block statement
• The finally block is used to write a block of code that
must execute, whether the try block raises an
exception or not
• Mostly, the finally block is used to release the external
resource
• This block provides a guarantee of execution
try/finally (Cont.)
try/finally (Cont.)
raise
• In Python, the raise statement allows to throw an
exception manually
• The single argument in the raise statement is an
exception to be raised
• It can be either an exception object or an Exception class
that is derived from the Exception class
• The raise statement is useful in situations where we need
to raise an exception explicitly
• For example, we can raise exceptions in cases such as
wrong data received or any validation failure
Steps To raise An Exception
• Give an exception of the appropriate type
• Use the existing built-in exceptions or create your own exception as
per the requirement
• Pass the appropriate data while raising an exception
• Execute a raise statement, by providing the exception
class
• Syntax:
raise (Example)
Custom Exceptions
• In Python, users can define custom exceptions by
creating a new class
• This exception class has to be derived from the built-
in Exception class
• Most of the built-in exceptions are also derived from this
class
Custom Exceptions (Example 1)
Custom Exceptions (Example 2)
Custom Exceptions (Example 3)

More Related Content

Similar to ACP - Week - 9.pptx

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
Raja Ram Dutta
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
raksharao
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
raksharao
 
exception handling.pptx
exception handling.pptxexception handling.pptx
exception handling.pptx
AbinayaC11
 
Exception handling in .net
Exception handling in .netException handling in .net
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
Bharath K
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
Nuha Noor
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Java class 7
Java class 7Java class 7
Java class 7Edureka!
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
Kunal Singh
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapriyankazope
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
sarthakgithub
 
Exception handling
Exception handlingException handling
Exception handling
pooja kumari
 
Exception
ExceptionException
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
Exception Handling in Python
Exception Handling in PythonException Handling in Python
Exception Handling in Python
DrJasmineBeulahG
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
MaddalaSeshu
 

Similar to ACP - Week - 9.pptx (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
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
exception handling.pptx
exception handling.pptxexception handling.pptx
exception handling.pptx
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
Java class 7
Java class 7Java class 7
Java class 7
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception
ExceptionException
Exception
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Exception Handling in Python
Exception Handling in PythonException Handling in Python
Exception Handling in Python
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
 
Exception
ExceptionException
Exception
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 

ACP - Week - 9.pptx

  • 1. ADVANCE COMPUTER PROGRAMMING Week 9 By: Ammarah Khalid (Lecturer CS) Riphah International University (Faisalabad)
  • 2. Exceptions • An exception is a condition which occurs during the execution of a program that disrupts the normal flow of the program's instructions • In general, when a Python script encounters a situation that it cannot cope with, it raises an exception • Whenever an exception occurs, the program stops the execution, and thus the further code is not executed • An exception is a Python object that represents an runtime error
  • 4. Common Exceptions • Python provides the number of built-in exceptions • A list of common exceptions that can be thrown from a standard Python: • ZeroDivisionError: It occurs when a number is divided by zero • NameError: It occurs when a name is not found. It may be local or global • ValueError: It occurs when there is an issue with the content of the object to which you wanted to assign a value • IOError: It occurs when Input Output operation fails • EOFError: It occurs when the end of the file is reached, and yet operations are being performed
  • 5. If Exception Is Not Handled..
  • 7. try/except • To handle exceptions we need to use try and except block • Define risky code that can raise an exception inside the try block and corresponding handling code inside the except block
  • 9. Using else With try/except • Use the else statement with the try-except statement in case to address the code which will be executed if no exception occurs in the try block
  • 10. Using else With try/except (Cont.)
  • 11. Using else With try/except (Cont.)
  • 12. Using else With try/except (Cont.) • Exception variable can also be used with except statement • It is done by using the as keyword • This object will return the cause of the exception
  • 13. Catching Specific Exception • A specific exception can also be caught • It is good practice to specify an exact exception that the except clause should catch • Suppose the user enters the denominator as zero • In that case, the try block will throw a ZeroDivisionError, and we can catch that
  • 14. Handling Multiple Exceptions • A piece of code can throw several different exceptions • It is needed to account for all of the potential exceptions that could be raised within try block • If the user enters a non-numeric value then, the try block will throw a ValueError exception
  • 15. Handling Multiple Exceptions (Cont.) • If the user enters the denominator as zero, the try block will throw a ZeroDivisionError • Thus it can be specified which exception except block should catch or handle • A try block can be followed by multiple numbers of except blocks to handle the different exceptions • But only one exception will be executed when an exception occurs
  • 16. Handling Multiple Exceptions (Cont.) • Multiple exceptions can also be handled with a single except clause • For that, use an tuple of values to specify multiple exceptions in an except clause
  • 17. try/finally • Python provides the finally block, which is used with the try block statement • The finally block is used to write a block of code that must execute, whether the try block raises an exception or not • Mostly, the finally block is used to release the external resource • This block provides a guarantee of execution
  • 20. raise • In Python, the raise statement allows to throw an exception manually • The single argument in the raise statement is an exception to be raised • It can be either an exception object or an Exception class that is derived from the Exception class • The raise statement is useful in situations where we need to raise an exception explicitly • For example, we can raise exceptions in cases such as wrong data received or any validation failure
  • 21. Steps To raise An Exception • Give an exception of the appropriate type • Use the existing built-in exceptions or create your own exception as per the requirement • Pass the appropriate data while raising an exception • Execute a raise statement, by providing the exception class • Syntax:
  • 23. Custom Exceptions • In Python, users can define custom exceptions by creating a new class • This exception class has to be derived from the built- in Exception class • Most of the built-in exceptions are also derived from this class