SlideShare a Scribd company logo
Advanced Python
Exception Handling in Python
An exception is an error which happens at the time of execution
of a program. However, while running a program, Python
generates an exception that should be handled to avoid your
program to crash. In Python language, exceptions trigger
automatically on errors, or they can be triggered and intercepted
by your code.
The exception indicates that, although the event can occur, this
type of event happens infrequently.When the method is not able
to handle the exception, it is thrown to its caller function.
Eventually, when an exception is thrown out of the main
function, the program is terminated abruptly.
What is an Exception in Python?
• Division by Zero
• Accessing a file which does not exist.
• Addition of two incompatible types
• Trying to access a nonexistent index of a sequence
• Removing the table from the disconnected database server.
• ATM withdrawal of more than the available amount
Common Examples of Exception:
• Exceptions must be class objects
• For class exceptions, you can use try statement with an except clause
which mentions a particular class.
• Even if a statement or expression is syntactically correct, it may
display an error when an attempt is made to execute it.
• Errors found during execution are called exceptions, and they are not
unconditionally fatal.
Rules of Exceptions
Exceptional Handling
Mechanism
try catch
finally throw
A try statement includes keyword try, followed by a colon (:)
and a suite of code in which exceptions may occur. It has one or
more clauses.
During the execution of the try statement, if no exceptions
occurred then, the interpreter ignores the exception handlers
for that specific try statement.
In case, if any exception occurs in a try suite, the try suite
expires and program control transfers to the matching except
handler following the try suite.
TheTry Statement
Catch blocks take one argument at a time, which is the type of exception
that it is likely to catch.These arguments may range from a specific type of
exception which can be varied to a catch-all category of exceptions.
The catch Statement
Rules for catch block:
• You can define a catch block by using the keyword catch
• Catch Exception parameter is always enclosed in parentheses
• It always represents the type of exception that catch block handles.
• An exception handling code is written between two {} curly braces.
• You can place multiple catch block within a single try block.
• You can use a catch block only after the try block.
• All the catch block should be ordered from subclass to superclass
exception.
Finally Block
Finally block always executes irrespective of an exception
being thrown or not. The final keyword allows you to create a
block of code that follows a try-catch block.
Finally, clause is optional. It is intended to define clean-up
actions which should be that executed in all conditions.
The Raise Statement
The raise statement specifies an argument which
exception object. Here, a comma follows the exception
and argument or tuple of the argument that follows
In this syntax, the argument is optional, and at the time
of execution, the exception argument value is always
none.
Example:
A Python exception can be any value like a string, class,
number, or an object. Most of these exceptions which are
raised by Python core are classes with an argument which
is an instance of the class.
Multithreading vs Multiprocessing
What's the difference?
A multiprocessing system has more than two processors.
The CPUs are added to the system that helps to increase
the computing speed of the system. Every CPU has its own
set of registers and main memory.
However, because each CPU are separate, it may happen
that one CPU may not have anything to process. One
processor may sit idle, and the other may be overloaded
with the specific processes. In such a case, the process and
resources are shared dynamically among the processors.
What is Multiprocessing?
Multithreading is a program execution technique
that allows a single process to have multiple code
segments (like threads). It also runs concurrently
within the "context" of that process. Multi-threaded
applications are applications that have two or more
threads that run concurrently.Therefore, it is also
known as concurrency.
What is Multithreading?
• A multiprocessing system has more than two processors whereas
Multithreading is a program execution technique that allows a single process
to have multiple code segments
• Multiprocessing improves the reliability of the system while in the
multithreading process, each thread runs parallel to each other.
• Multiprocessing helps you to increase computing power whereas
multithreading helps you create computing threads of a single process
• In Multiprocessing, the creation of a process, is slow and resource-specific
whereas, in Multiprogramming, the creation of a thread is economical in time
and resource.
• Multithreading avoids pickling, whereas Multiprocessing relies on pickling
objects in memory to send to other processes.
• Multiprocessing system takes less time whereas for job processing a moderate
amount of time is taken.
Key DIFFERENCES:
• The biggest advantage of a multiprocessor system is that it helps you to get
more work done in a shorter period.
• The code is usually straightforward.
• Takes advantage of multipleCPU & cores
• Helps you to avoid GIL limitations for CPython
• Remove synchronization primitives unless if you use shared memory.
• Child processes are mostly interruptible/killable
• It helps you to get work done in a shorter period.
• These types of systems should be used when very high speed is required to
process a large volume of data.
• Multiprocessing systems save money compared to single processor systems as
processors can share peripherals and power supplies.
Advantage of Multiprocessing
Disadvantage of
Multiprocessing
• IPC(Inter-Process Communication) a quite complicated
with more overhead
• Has a larger memory footprint
• Threads share the same address space
• Threads are lightweight which has a low memory footprint
• The cost of communication between threads is low.
• Access to memory state from another context is easier
• It allows you to make responsive UIs easily
• An ideal option for I/O-bound applications
• Takes lesser time to switch between two threads within the shared
memory and time to terminate
• Threads are faster to start than processes and also faster in task-
switching.
• AllThreads share a process memory pool that is very beneficial.
• Takes lesser time to create a new thread in the existing process than a
new process
Advantage of Multithreading
• Multithreading system is not interruptible/killable
• If not following a command queue and message pump model
then manual use of synchronization needed which becomes a
necessity
• Code is usually harder to understand and increases the potential
for race conditions increases dramatically
Disadvantage of multithreading

More Related Content

PDF
Cloud Computing Was Built for Web Developers—What Does v2 Look Like for Deep...
PDF
Building A Machine Learning Platform At Quora (1)
PDF
Deploying and Monitoring Heterogeneous Machine Learning Applications with Cli...
PDF
Deep Learning for Natural Language Processing Using Apache Spark and TensorFl...
PDF
Bringing Deep Learning into production
PDF
Anomaly Detection and Automatic Labeling with Deep Learning
PPTX
Managing and Versioning Machine Learning Models in Python
PDF
CI/CD for Machine Learning with Daniel Kobran
Cloud Computing Was Built for Web Developers—What Does v2 Look Like for Deep...
Building A Machine Learning Platform At Quora (1)
Deploying and Monitoring Heterogeneous Machine Learning Applications with Cli...
Deep Learning for Natural Language Processing Using Apache Spark and TensorFl...
Bringing Deep Learning into production
Anomaly Detection and Automatic Labeling with Deep Learning
Managing and Versioning Machine Learning Models in Python
CI/CD for Machine Learning with Daniel Kobran

What's hot (20)

PPTX
Justin Basilico, Research/ Engineering Manager at Netflix at MLconf SF - 11/1...
PDF
Modern web dev_taxonomy
PDF
ML at the Edge: Building Your Production Pipeline with Apache Spark and Tens...
PDF
MLOps with Kubeflow
PDF
Strata Beijing 2017: Jumpy, a python interface for nd4j
PPTX
Dl4j in the wild
PDF
MLOps - Build pipelines with Tensor Flow Extended & Kubeflow
PDF
Continuous Delivery Amsterdam - Microservices in action at the Dutch National...
PDF
J-Spring 2017 - Microservices in action at the Dutch National Police
PDF
Javantura v4 - FreeMarker in Spring web - Marin Kalapać
PDF
Productionizing H2O Models with Apache Spark with Jakub Hava and Michal Maloh...
PDF
Using Apache Spark for Predicting Degrading and Failing Parts in Aviation
PDF
MLflow with R
PDF
From Prototyping to Deployment at Scale with R and sparklyr with Kevin Kuo
ODP
Self driving computers active learning workflows with human interpretable ve...
PDF
Deep learning in production with the best
PDF
SKIL - Dl4j in the wild meetup
PDF
Deep Learning with GPUs in Production - AI By the Bay
PDF
The Polyglot Data Scientist - Exploring R, Python, and SQL Server
PDF
Deploying End-to-End Deep Learning Pipelines with ONNX
Justin Basilico, Research/ Engineering Manager at Netflix at MLconf SF - 11/1...
Modern web dev_taxonomy
ML at the Edge: Building Your Production Pipeline with Apache Spark and Tens...
MLOps with Kubeflow
Strata Beijing 2017: Jumpy, a python interface for nd4j
Dl4j in the wild
MLOps - Build pipelines with Tensor Flow Extended & Kubeflow
Continuous Delivery Amsterdam - Microservices in action at the Dutch National...
J-Spring 2017 - Microservices in action at the Dutch National Police
Javantura v4 - FreeMarker in Spring web - Marin Kalapać
Productionizing H2O Models with Apache Spark with Jakub Hava and Michal Maloh...
Using Apache Spark for Predicting Degrading and Failing Parts in Aviation
MLflow with R
From Prototyping to Deployment at Scale with R and sparklyr with Kevin Kuo
Self driving computers active learning workflows with human interpretable ve...
Deep learning in production with the best
SKIL - Dl4j in the wild meetup
Deep Learning with GPUs in Production - AI By the Bay
The Polyglot Data Scientist - Exploring R, Python, and SQL Server
Deploying End-to-End Deep Learning Pipelines with ONNX
Ad

Similar to Advanced python (20)

PPTX
Scheduling Thread
PDF
PyCon Canada 2019 - Introduction to Asynchronous Programming
PPTX
PPTX
Exception handling in ASP .NET
PDF
Concurrency in Operating system_12345678
PPTX
Threads, signal and socket system calls.pptx
PPTX
High Reliabilty Systems
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Operating system 27 semaphores
PPTX
Connection Resiliency and Command Interception in Entity Framework
PPTX
OOP with Java - Multithreading - Module-5.pptx
PDF
Programming with C++
PPTX
Multi threading
PPTX
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Scheduling Thread
PyCon Canada 2019 - Introduction to Asynchronous Programming
Exception handling in ASP .NET
Concurrency in Operating system_12345678
Threads, signal and socket system calls.pptx
High Reliabilty Systems
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Operating system 27 semaphores
Connection Resiliency and Command Interception in Entity Framework
OOP with Java - Multithreading - Module-5.pptx
Programming with C++
Multi threading
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Ad

More from Novita Sari (13)

PDF
Summary python coding
PDF
Summary machine learning and model deployment
PDF
Summary legal
PDF
Summary introduction to data engineering
PDF
Summary data visualization
PDF
Summary data modelling
PDF
Summary business knowledge for data professional
PDF
Practice case legal for data professional
PDF
Big data tools
PPTX
Git, Docker, Python Package and Module
PDF
OOP, Networking, Linux/Unix
PDF
Python Function and Looping
PDF
Basic Data Engineering
Summary python coding
Summary machine learning and model deployment
Summary legal
Summary introduction to data engineering
Summary data visualization
Summary data modelling
Summary business knowledge for data professional
Practice case legal for data professional
Big data tools
Git, Docker, Python Package and Module
OOP, Networking, Linux/Unix
Python Function and Looping
Basic Data Engineering

Recently uploaded (20)

PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
Computer network topology notes for revision
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Mega Projects Data Mega Projects Data
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
IBA_Chapter_11_Slides_Final_Accessible.pptx
Qualitative Qantitative and Mixed Methods.pptx
Database Infoormation System (DBIS).pptx
Business Acumen Training GuidePresentation.pptx
IB Computer Science - Internal Assessment.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
STUDY DESIGN details- Lt Col Maksud (21).pptx
Data_Analytics_and_PowerBI_Presentation.pptx
Computer network topology notes for revision
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Mega Projects Data Mega Projects Data
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
1_Introduction to advance data techniques.pptx
Reliability_Chapter_ presentation 1221.5784
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Business Ppt On Nestle.pptx huunnnhhgfvu
Acceptance and paychological effects of mandatory extra coach I classes.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj

Advanced python

  • 3. An exception is an error which happens at the time of execution of a program. However, while running a program, Python generates an exception that should be handled to avoid your program to crash. In Python language, exceptions trigger automatically on errors, or they can be triggered and intercepted by your code. The exception indicates that, although the event can occur, this type of event happens infrequently.When the method is not able to handle the exception, it is thrown to its caller function. Eventually, when an exception is thrown out of the main function, the program is terminated abruptly. What is an Exception in Python?
  • 4. • Division by Zero • Accessing a file which does not exist. • Addition of two incompatible types • Trying to access a nonexistent index of a sequence • Removing the table from the disconnected database server. • ATM withdrawal of more than the available amount Common Examples of Exception:
  • 5. • Exceptions must be class objects • For class exceptions, you can use try statement with an except clause which mentions a particular class. • Even if a statement or expression is syntactically correct, it may display an error when an attempt is made to execute it. • Errors found during execution are called exceptions, and they are not unconditionally fatal. Rules of Exceptions
  • 7. A try statement includes keyword try, followed by a colon (:) and a suite of code in which exceptions may occur. It has one or more clauses. During the execution of the try statement, if no exceptions occurred then, the interpreter ignores the exception handlers for that specific try statement. In case, if any exception occurs in a try suite, the try suite expires and program control transfers to the matching except handler following the try suite. TheTry Statement
  • 8. Catch blocks take one argument at a time, which is the type of exception that it is likely to catch.These arguments may range from a specific type of exception which can be varied to a catch-all category of exceptions. The catch Statement Rules for catch block: • You can define a catch block by using the keyword catch • Catch Exception parameter is always enclosed in parentheses • It always represents the type of exception that catch block handles. • An exception handling code is written between two {} curly braces. • You can place multiple catch block within a single try block. • You can use a catch block only after the try block. • All the catch block should be ordered from subclass to superclass exception.
  • 9. Finally Block Finally block always executes irrespective of an exception being thrown or not. The final keyword allows you to create a block of code that follows a try-catch block. Finally, clause is optional. It is intended to define clean-up actions which should be that executed in all conditions.
  • 10. The Raise Statement The raise statement specifies an argument which exception object. Here, a comma follows the exception and argument or tuple of the argument that follows In this syntax, the argument is optional, and at the time of execution, the exception argument value is always none. Example: A Python exception can be any value like a string, class, number, or an object. Most of these exceptions which are raised by Python core are classes with an argument which is an instance of the class.
  • 12. A multiprocessing system has more than two processors. The CPUs are added to the system that helps to increase the computing speed of the system. Every CPU has its own set of registers and main memory. However, because each CPU are separate, it may happen that one CPU may not have anything to process. One processor may sit idle, and the other may be overloaded with the specific processes. In such a case, the process and resources are shared dynamically among the processors. What is Multiprocessing?
  • 13. Multithreading is a program execution technique that allows a single process to have multiple code segments (like threads). It also runs concurrently within the "context" of that process. Multi-threaded applications are applications that have two or more threads that run concurrently.Therefore, it is also known as concurrency. What is Multithreading?
  • 14. • A multiprocessing system has more than two processors whereas Multithreading is a program execution technique that allows a single process to have multiple code segments • Multiprocessing improves the reliability of the system while in the multithreading process, each thread runs parallel to each other. • Multiprocessing helps you to increase computing power whereas multithreading helps you create computing threads of a single process • In Multiprocessing, the creation of a process, is slow and resource-specific whereas, in Multiprogramming, the creation of a thread is economical in time and resource. • Multithreading avoids pickling, whereas Multiprocessing relies on pickling objects in memory to send to other processes. • Multiprocessing system takes less time whereas for job processing a moderate amount of time is taken. Key DIFFERENCES:
  • 15. • The biggest advantage of a multiprocessor system is that it helps you to get more work done in a shorter period. • The code is usually straightforward. • Takes advantage of multipleCPU & cores • Helps you to avoid GIL limitations for CPython • Remove synchronization primitives unless if you use shared memory. • Child processes are mostly interruptible/killable • It helps you to get work done in a shorter period. • These types of systems should be used when very high speed is required to process a large volume of data. • Multiprocessing systems save money compared to single processor systems as processors can share peripherals and power supplies. Advantage of Multiprocessing
  • 16. Disadvantage of Multiprocessing • IPC(Inter-Process Communication) a quite complicated with more overhead • Has a larger memory footprint
  • 17. • Threads share the same address space • Threads are lightweight which has a low memory footprint • The cost of communication between threads is low. • Access to memory state from another context is easier • It allows you to make responsive UIs easily • An ideal option for I/O-bound applications • Takes lesser time to switch between two threads within the shared memory and time to terminate • Threads are faster to start than processes and also faster in task- switching. • AllThreads share a process memory pool that is very beneficial. • Takes lesser time to create a new thread in the existing process than a new process Advantage of Multithreading
  • 18. • Multithreading system is not interruptible/killable • If not following a command queue and message pump model then manual use of synchronization needed which becomes a necessity • Code is usually harder to understand and increases the potential for race conditions increases dramatically Disadvantage of multithreading