0% found this document useful (0 votes)
13 views3 pages

Module 5 File - Exception Handlina

Uploaded by

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

Module 5 File - Exception Handlina

Uploaded by

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

MODULE – 5 ASSIGNMENT

File Handling & Exception Handling

1)Write the code in python to open a file named “try.txt”

2)What is the purpose of ‘r’ as a prefix in the given statement?

f = open(r, “d:\color\flower.txt”)

3)Write a note on the following

A. Purpose of Exception Handling


B. Try block
C. Except block
D. Else block
E. Finally block
F. Built-in exceptions

4) Write 2 Custom exceptions

ANSWERS

1. file = open("try.txt", "r")

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)

© 360DigiTMG. All Rights Reserved.


A) Exception handling is a technique in Python for dealing with errors that occur during
program execution. It entails spotting potential error situations, responding appropriately to
exceptions when they arise, and identifying possible error conditions.

b) The try block lets you test a block of code for errors.

c) The except block lets you handle the error.

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:

print("Something went wrong")

finally:

print("The 'try except' is finished")

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.

© 360DigiTMG. All Rights Reserved.


© 360DigiTMG. All Rights Reserved.

You might also like