SlideShare une entreprise Scribd logo
1  sur  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)

Contenu connexe

Similaire à ACP - Week - 9.pptx

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
 
Java class 7
Java class 7Java class 7
Java class 7
Edureka!
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
priyankazope
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
Kumar
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
MaddalaSeshu
 

Similaire à 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
 

Dernier

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Dernier (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 

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