SlideShare una empresa de Scribd logo
1 de 26
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)

Más contenido relacionado

Similar a 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.pptRaja Ram Dutta
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handlingraksharao
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingraksharao
 
exception handling.pptx
exception handling.pptxexception handling.pptx
exception handling.pptxAbinayaC11
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha 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 allHayomeTakele
 
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 JAVAKunal 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
 
Exception handling
Exception handlingException handling
Exception handlingpooja kumari
 
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 PythonDrJasmineBeulahG
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdfMaddalaSeshu
 

Similar a 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
 

Último

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Último (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

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