Professional Documents
Culture Documents
f = open(r, “d:\color\flower.txt”)
ANSWERS
file_1=open(r"C:\Users\admin\.spyder-py3\try.txt")
print(file_1.read())
Exception handling is the process of responding to unwanted or unexpected events when a computer
program runs. Exception handling deals with these events to avoid the program or system crashing, and
without this process, exceptions would disrupt the normal operation of a program.
The try block lets you test a block of code for errors. The except block lets you handle the error. The else
block lets you execute code when there is no error. The finally block lets you execute code, regardless of
the result of the try- and except blocks.
The except block lets you handle the error. The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the try- and except blocks.
An else statement can be combined with an if statement. An else statement contains the block of code
that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else
statement is an optional statement and there could be at most only one else statement following if.
It defines a block of code to run when the try... except...else block is final. The finally block will be
executed no matter if the try block raises an error or not. This can be useful to close objects and clean
up resources.
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 SalaryNotInRangeError(Exception):
Attributes:
"""
self.salary = salary
self.message = message
super().__init__(self.message)
salary = int(input("Enter salary amount: "))
raise SalaryNotInRangeError(salary)
class InvalidAgeException(Exception):
pass
number = 18
try:
raise InvalidAgeException
else:
print("Eligible to Vote")
except InvalidAgeException: