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

What's hot (20)

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

Similar to Advanced python (20)

PPTX
Scheduling Thread
MuhammadBilal187526
 
PDF
PyCon Canada 2019 - Introduction to Asynchronous Programming
Juti Noppornpitak
 
PPTX
Exception handling in .net
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Concurrency in Operating system_12345678
ankitashah871482
 
PPTX
Threads, signal and socket system calls.pptx
JohnNderi1
 
PPTX
High Reliabilty Systems
LloydMoore
 
PPTX
Concurrency with java
James Wong
 
PPTX
Concurrency with java
Luis Goldster
 
PPTX
Concurrency with java
Young Alista
 
PPTX
Concurrency with java
Hoang Nguyen
 
PPTX
Concurrency with java
Fraboni Ec
 
PPTX
Concurrency with java
Tony Nguyen
 
PPTX
Concurrency with java
Harry Potter
 
PPTX
Operating system 27 semaphores
Vaibhav Khanna
 
PPTX
Connection Resiliency and Command Interception in Entity Framework
Muhammad Umar
 
PDF
Programming with C++
ssuser802d47
 
PPTX
Multi threading
gndu
 
PPTX
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Sachintha Gunasena
 
PPTX
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
KesavanT10
 
Scheduling Thread
MuhammadBilal187526
 
PyCon Canada 2019 - Introduction to Asynchronous Programming
Juti Noppornpitak
 
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Concurrency in Operating system_12345678
ankitashah871482
 
Threads, signal and socket system calls.pptx
JohnNderi1
 
High Reliabilty Systems
LloydMoore
 
Concurrency with java
James Wong
 
Concurrency with java
Luis Goldster
 
Concurrency with java
Young Alista
 
Concurrency with java
Hoang Nguyen
 
Concurrency with java
Fraboni Ec
 
Concurrency with java
Tony Nguyen
 
Concurrency with java
Harry Potter
 
Operating system 27 semaphores
Vaibhav Khanna
 
Connection Resiliency and Command Interception in Entity Framework
Muhammad Umar
 
Programming with C++
ssuser802d47
 
Multi threading
gndu
 
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Sachintha Gunasena
 
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
KesavanT10
 
Ad

More from Novita Sari (13)

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

Recently uploaded (20)

PDF
Choosing the Right Database for Indexing.pdf
Tamanna
 
PPTX
Introduction to Artificial Intelligence.pptx
StarToon1
 
PDF
Incident Response and Digital Forensics Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Hadoop_EcoSystem slide by CIDAC India.pptx
migbaruget
 
PPTX
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PDF
Early_Diabetes_Detection_using_Machine_L.pdf
maria879693
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PPT
Lecture 2-1.ppt at a higher learning institution such as the university of Za...
rachealhantukumane52
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PPTX
Rational Functions, Equations, and Inequalities (1).pptx
mdregaspi24
 
PPTX
加拿大尼亚加拉学院毕业证书{Niagara在读证明信Niagara成绩单修改}复刻
Taqyea
 
PDF
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PPTX
Resmed Rady Landis May 4th - analytics.pptx
Adrian Limanto
 
PPTX
Rocket-Launched-PowerPoint-Template.pptx
Arden31
 
PDF
2_Management_of_patients_with_Reproductive_System_Disorders.pdf
motbayhonewunetu
 
Choosing the Right Database for Indexing.pdf
Tamanna
 
Introduction to Artificial Intelligence.pptx
StarToon1
 
Incident Response and Digital Forensics Certificate
VICTOR MAESTRE RAMIREZ
 
Hadoop_EcoSystem slide by CIDAC India.pptx
migbaruget
 
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
Early_Diabetes_Detection_using_Machine_L.pdf
maria879693
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
Lecture 2-1.ppt at a higher learning institution such as the university of Za...
rachealhantukumane52
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
Rational Functions, Equations, and Inequalities (1).pptx
mdregaspi24
 
加拿大尼亚加拉学院毕业证书{Niagara在读证明信Niagara成绩单修改}复刻
Taqyea
 
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
Resmed Rady Landis May 4th - analytics.pptx
Adrian Limanto
 
Rocket-Launched-PowerPoint-Template.pptx
Arden31
 
2_Management_of_patients_with_Reproductive_System_Disorders.pdf
motbayhonewunetu
 

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