Module 5 File - Exception Handlina
Module 5 File - Exception Handlina
f = open(r, “d:\color\flower.txt”)
ANSWERS
2. In the given statement, the 'r' as a prefix before the file path string indicates that the string
should be treated as a raw string literal. This means that backslashes () in the string will be
treated as literal characters rather than as escape characters.
For instance, consider the file path "d:\color\flower.txt". Without the 'r' prefix, Python would
interpret '\c' as an escape sequence, which is invalid. To prevent this, you can use a raw string
literal by adding the 'r' prefix before the string. This tells Python to treat the string exactly as it
is written, without interpreting any escape sequences.
3)
b) The try block lets you test a block of code for errors.
d) The else block lets you execute code when there is no error.
e) The finally block lets you execute code, regardless of the result of the try- and except blocks.
try:
print(x)
except:
finally:
f) 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.
4) InvalidEmailError:
This custom exception can be used to raise an error when an invalid email address is
Encountered.
InsuffcientBalanceError:
This custom exception can be used in a banking application to raise an error when an account
has insuffcient balance or a transaction.