Module 5 File - Exception Handling
Module 5 File - Exception Handling
Module 5 File - Exception Handling
txt”
file.close()
# B. Try block
# Reply : In Python, the try block is used to enclose code that might raise an
exception.
# If an exception is raised within the try block, the code in the
corresponding except block is executed.
# C. Except block
# Reply : Python provides a way to catch specific exceptions during a program's
execution.
# This is done with the "try" statement
# D. Else block
# Reply : The code inside the try block is executed until an exception is raised.
# If an exception is raised, the code inside the except block is executed.
# The code inside the else block is executed if no exception is raised.
# E. Finally block
# Reply : Finally and Else are two keywords used in try..except blocks in Python.
# The Finally block executes a set of statements, regardless of the result
of the try..except blocks.
# This is useful when you want to clean up resources like closing a file or
connection, irrespective of whether an exception occurred or not.
# The Else block is used to execute a set of statements only if the try
block does not raise an exception.
# It is useful for code that must be executed if the try block does not
raise an exception.
# F. Built-in exceptions
# Reply : Python Built-in Exceptions. Python has a number of built-in exceptions,
# such as the well-known errors SyntaxError, NameError, and TypeError.
# These Python Exceptions are thrown by standard library routines or by the
interpreter itself.
# They are built-in, which implies they are present in the source code at
all times.
class MyCustomError(Exception):