0% found this document useful (0 votes)
30 views7 pages

Handling Multiple Exceptions in Python

The document discusses exception handling in Python. It describes the try, except, else, and finally blocks used to handle exceptions. The try block contains code that may raise exceptions. The except block handles the exceptions. The else block executes if no exceptions occur. The finally block always executes regardless of exceptions. It also discusses built-in and user-defined exceptions as well as raising and asserting exceptions.

Uploaded by

ananya
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)
30 views7 pages

Handling Multiple Exceptions in Python

The document discusses exception handling in Python. It describes the try, except, else, and finally blocks used to handle exceptions. The try block contains code that may raise exceptions. The except block handles the exceptions. The else block executes if no exceptions occur. The finally block always executes regardless of exceptions. It also discusses built-in and user-defined exceptions as well as raising and asserting exceptions.

Uploaded by

ananya
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

ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

HANDLING EXCEPTION
Exception
An Exception is an event which occurs during the execution of a program that disrupts the normal
flow of the program’s instructions. If a python script encounters a situation it cannot cope up, it
raises an exception
HOW TO HANDLE EXCEPTION?
There are four blocks which help to handle the exception. They are
 try block
 except statement
 else block
 finally block
i) try block
 In the try block a programmer will write the suspicious code that may raise an
exception. One can defend their program from a run time error by placing the codes
inside the try block.
Syntax:
try:
#The operations here

ii) except statement


 Except statement should be followed by the try block.
 A single try block can have multiple except statement.
 The except statement which handles the exception.
 Multiple except statements require multiple exception names to handle the exception
separately.

except Exception 1:
#Handle Exception 1
except Exception 2:
#Handle Exception 2

iii) else block


If there is no exception in the program the else block will get executed.
Syntax:
else:
#If no Exception, it will execute
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

iv) finally block:


A finally block will always execute whether an exception happens or not the block will
always execute.

finally:
#Always Execute

Syntax:

(i) Write a python program to write a file with exception handling.

(ii) Write a python program to read a file which raises an exception


Assume [Link] is not created in the computer and the following program is executed
which raises an exception.

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

a) Except Clause with no exception


An except statement without the exception name, the python interpreter will consider
as default ‘Exception’ and it will catch all exceptions.
Syntax:

b) Except clause with multiple Exception


An except statement can have multiple exceptions, We call it by exception name
Syntax:

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

(i) Write a python program to read a file with multiple exceptions

c) Argument of an Exception
An exception can have an argument which is a value that gives additional information
about the problem. The content of an argument will vary by the exception.

d) Raising an Exception
You can raise exceptions in several ways by using raise statement
Syntax:

Example:

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

TYPES OF EXCEPTION
There are two types of Exception:
 Built-in Exception
 User Defined Exception
i) Built-in Exception
There are some built-in exceptions which should not be changed.
The Syntax for all Built-in Exception

except Exception_Name

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

USER DEFINED EXCEPTION


In Python a user can create their own exception by deriving classes from standard
Exceptions. There are two steps to create a user defined exception.
Step-1
A User Defined Exception should be derived from standard Built-in Exceptions.
Step-2
After referring base class the user defined exception can be used in the program

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING


ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY

ASSERTION
An assertion is a sanity check which can turn on (or) turn off when the program is in testing mode.
 The assertion can be used by assert keyword. It will check whether the input is valid and
after the operation it will check for valid output.
Syntax:

assert(Expression)

Example def add(x):


assert(x<0)
return(x)
add(10)

GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING

Common questions

Powered by AI

Raising exceptions manually can proactively control error detection by allowing a program to exit from erroneous states decisively. It is beneficial when certain conditions detected by the program's logic require immediate attention or correction beyond standard flow. For instance, in a user registration system, if input validation reveals duplicate usernames, a 'UsernameExistsError' can be raised, notifying the caller about the specific issue well beyond built-in validation checks. This ensures fine-grained control over application-specific errors and aligns system responses with business logic. Such error management streamlines user feedback and system responses, enhancing user experience and reliability.

Built-in exceptions are predefined in Python and cover common error situations, such as 'TypeError', 'ValueError', or 'ZeroDivisionError'. User-defined exceptions are custom exceptions created by subclassing the built-in 'Exception' class to handle specific application errors that aren't adequately covered by built-in exceptions. While built-in exceptions enhance basic error checking, user-defined exceptions increase a program's robustness by providing tailored error information and handling strategies, thus supporting greater precision in debugging and ensuring explicit control over error conditions applicable to the specific application's context.

User-defined exceptions in Python are created by deriving a new class from the standard built-in 'Exception' class. This is done by defining a new class and referring to a base class for initialization. For instance: 'class MyCustomError(Exception): pass'. These exceptions are necessary when a specific error arises in the application that is not covered by built-in exceptions, allowing for more clarity and specificity in error handling. An example scenario could be a banking application where a 'NegativeBalanceError' might be defined to capture attempts to withdraw more money than available in an account, providing precise feedback to the developer or end-user.

A programmer can handle multiple exceptions in a single block of code using the except clause with multiple exception names. This is done by specifying the exception names as a tuple within a single except statement. For example, "except (IOError, EOFError):" will handle both IOError and EOFError exceptions in the same block. This approach simplifies the code by avoiding repetitive except blocks for similar error handling scenarios.

An assertion in Python acts as a debugging aid, implemented using the 'assert' keyword. It is used to verify that a certain condition is true during program execution. If the condition evaluates as false, an AssertionError is raised, and an optional error message can be provided. Assertions are primarily used as a sanity check to catch critical errors more quickly during development and reduce their impact on production systems. They validate the program's logic by checking whether certain conditions hold and are often disabled in production by running Python with the '-O' (optimize) switch.

An 'except' clause with no specific exception acts as a catch-all handler in Python. For example: ```python try: # code that may raise an exception x = 1 / 0 except: print("An exception occurred") ``` In the code above, dividing by zero raises a 'ZeroDivisionError', but due to the general 'except' clause, it catches this exception and executes the print statement. This catch-all mechanism is useful for logging general issues but should be used cautiously to avoid hindering error diagnostics by masking diverse error types.

The try-except-finally-else construct in Python is a mechanism used to manage exceptions that occur during the execution of a program. The 'try' block contains code that might raise an exception. The 'except' block captures and handles the exception if an error occurs in the try block. A single try block can have multiple except clauses, allowing for specific handling based on the exception type. The 'finally' block contains code that executes regardless of whether an exception was raised or not—it is used for cleanup actions like closing files or releasing resources. If no exceptions are raised, the 'else' block will execute after the try block.

In Python, exceptions can have arguments, which are values that provide additional information about an error. These arguments can be accessed in the except block to gather more context about the issue encountered. When an exception is raised, its argument could be an error message or additional details that can help diagnose the problem. This adds robustness to exception handling, allowing error messages to be customized and providing more informative feedback for debugging and logging, ultimately aiding developers in tracing issues efficiently.

The 'finally' block in exception handling is essential because it ensures that certain code is executed regardless of whether an exception occurs or not. This is particularly important for operations involving external resources like file handling or network connections, where failing to release resources can lead to resource leaks. By placing cleanup code such as closing files or network connections in the 'finally' block, a programmer ensures these operations are completed and system resources are released, preventing potential memory leaks or resource deadlocks.

The 'else' block in a try-except-else-finally structure is preferable when there is a need to execute code only if no exceptions were raised in the try block. It enhances code readability and ensures that this specific block is executed only under successful completion of code within the try block, thereby separating successful execution logic from the main try-except structure. This can help in avoiding potential logical errors and makes the codebase cleaner and easier to maintain. For instance, additional verification steps or further processing of successfully retrieved data can be done in the else block.

You might also like