0% found this document useful (0 votes)
1 views

016. Exception Handling in Python - Web (1)

The document explains exception handling in Python, which is crucial for managing runtime errors that disrupt program execution. It details the use of try and except blocks to handle errors gracefully, as well as the finally block for cleanup actions. Various examples illustrate how to handle specific exceptions and multiple exceptions effectively.

Uploaded by

snp.arts69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

016. Exception Handling in Python - Web (1)

The document explains exception handling in Python, which is crucial for managing runtime errors that disrupt program execution. It details the use of try and except blocks to handle errors gracefully, as well as the finally block for cleanup actions. Various examples illustrate how to handle specific exceptions and multiple exceptions effectively.

Uploaded by

snp.arts69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Exception Handling in Python

“Handling the runtime errors”

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
What are Exceptions?
• an exception is an event that occurs during
the execution of a program that disrupts the
normal flow of instructions.
• These exceptions can occur for various
reasons, such as invalid user input, file not
found, or division by zero.

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
Handling Exceptions
• It is a powerful mechanism to handle the
runtime errors so that the normal flow of
application can be maintained.
• Since exception abnormally terminate the
execution of a program, it is important to
handle the exceptions. In Python we use try
and except block to handle the exception.
Another keyword finally can also be used to
perform cleanup task.

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
try block
• try block lets you test a block of code for
errors.
• We put all those codes inside the try block
which may raise runtime errors.
• When try block encounters any runtime error
the control is passed to appropriate except
block.

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
except block
• Except block lets you handle runtime errors
• When any runtime error occurs in try block
the control is transferred to except block to
handle and maintain the normal execution of
program
• Default “except” block can handle any type of
runtime errors however we can pass the name
of runtime error with except block to handle
specific type of runtime error.

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
Example – 1 (Handling Divide by Zero)

When we input
valid data, program
will execute
successfully

If denominator is
entered as 0, then
program will crash
with runtime error
“ZeroDivisionError”
Prepared by - Vinod Kumar Verma, PGT(CS)
, Sachin Bhardwaj PGT(CS)
Example – 2 (Handling Divide by Zero)

When we input
valid data, program
will execute
successfully

If denominator is entered
as 0, then program will
not abnormally terminate
and error is handled
gracefully
Prepared by - Vinod Kumar Verma, PGT(CS)
, Sachin Bhardwaj PGT(CS)
Example – 2 (Handling Divide by Zero)

However, here we used except


block which can handle any type
of runtime errors, so if try block
raises any other runtime error like
ValueError when we input invalid
data in variable a or b like string,
the same message ‘Sorry you
cannot divide by zero’ will be
Prepared by - Vinod Kumar Verma, PGT(CS)
printed
, Sachin Bhardwaj PGT(CS)
Example – 3 (Handling specific Exception)

Name of specific
exception is given

Successful execution with valid data

Divide by 0 error is handled successfully

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
Example – 3 (Handling specific Exception)

As only ZeroDivisionError exception is handle here, if any other runtime error occurs like if
we input string data in place ofPrepared
int data, thenKumar
by - Vinod program will crash with ValueError Exception
Verma, PGT(CS)
, Sachin Bhardwaj PGT(CS)
Example – 4 (Multiple Exception Handling)

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
Example – 4 (Multiple Exception Handling)

Successful Execution

Divide by Zero Case

If wrong input supplied

Occurs because variable d is not defined and


assigned, NameError Exception will occur
and will be handled by except block
Prepared by - Vinod Kumar Verma, PGT(CS)
, Sachin Bhardwaj PGT(CS)
finally block
• The 'finally' block of python is always executed
whether an exception in code occurred or
not.
• It allows us to perform clean-up actions like
releasing resources or closing files,
irrespective of the presence of exceptions.

Prepared by - Vinod Kumar Verma, PGT(CS)


, Sachin Bhardwaj PGT(CS)
Example (with finally block)

Here we can see


that code of finally
block executed in
both cases, whether
exception occurred
Prepared by - Vinod Kumar Verma, PGT(CS) on not
, Sachin Bhardwaj PGT(CS)
Points to remember
• try block must be followed by either except or
finally block
• except block is not mandatory with try block, we
can write either except or finally. However to
handle error for successful execution of program
except block is must.
• While handling multiple exception the except
block must be the last block otherwise all other
exception given below except will become
unreachable. Syntax error will appear with
message ‘default except must be last’
Prepared by - Vinod Kumar Verma, PGT(CS)
, Sachin Bhardwaj PGT(CS)

You might also like