RBSE Class 12 Computer Science Chapter 1 Question and Answers
Chapter 1 Exception Handling in Python Questions and Answers
Question1.
“Every syntax error is an exception but every
exception cannot be a syntax error.” Justify the statement.( "เคนเคฐ เคธिंเคैเค्เคธ เคค्เคฐुเคि เคเค เค
เคชเคตाเคฆ เคนै เคฒेเคिเคจ เคนเคฐ เค
เคชเคตाเคฆ เคธिंเคैเค्เคธ เคค्เคฐुเคि เคจเคนीं เคนो เคธเคเคคा เคนै।" เคเคฅเคจ เคा เคเคिเคค्เคฏ เคธिเคฆ्เคง เคीเคिเค।)
A. Syntax error ar e errors which occurs by not following
the proper structure which leads to exception, while exceptions are IOError
(Input Output Error), ImportError, ValueError.
เคเคค्เคคเคฐ - (เคธिंเคैเค्เคธ เคค्เคฐुเคि เคตे เคค्เคฐुเคिเคฏां เคนैं เคो เคเคिเคค เคธंเคฐเคเคจा เคा เคชाเคฒเคจ เคจ เคเคฐเคจे เคธे เคนोเคคी เคนैं เคो เค
เคชเคตाเคฆ เคी เคเคฐ เคฒे เคाเคคी เคนैं, เคเคฌเคि เค
เคชเคตाเคฆ IOError
(Input Output Error), ImportError, ValueError เคนैं।)
Exception :- An exception is an occurrence during the execution of
a program that causes the usual flow of the program's instructions to be
disrupted.
เค
เคชเคตाเคฆ :- (เคเค
เค
เคชเคตाเคฆ เคเค
เคช्เคฐोเค्เคฐाเคฎ เคे
เคจिเคท्เคชाเคฆเคจ เคे
เคฆौเคฐाเคจ เคเค
เคเคเคจा เคนै
เคो เคช्เคฐोเค्เคฐाเคฎ เคे เคจिเคฐ्เคฆेเคถों เคे เคธाเคฎाเคจ्เคฏ เคช्เคฐเคตाเคน เคो เคฌाเคงिเคค เคเคฐเคจे เคा เคाเคฐเคฃ เคฌเคจเคคा เคนै।)
Syntax errors are detected when we have not followed the rules of
the particular programming language while writing a program and that may cause
of arising an exception, while exception can occur even without any syntax
error.
For
example exception like trying to open a file which does not exist, division by
zero, etc. so this is how every syntax error is an exception but
every exception cannot be a syntax error.
(เคธिंเคैเค्เคธ เคค्เคฐुเคिเคฏों เคा เคชเคคा เคคเคฌ เคเคฒเคคा เคนै เคเคฌ เคนเคฎเคจे เคช्เคฐोเค्เคฐाเคฎ เคฒिเคเคคे เคธเคฎเคฏ เคตिเคถेเคท เคช्เคฐोเค्เคฐाเคฎिंเค เคญाเคทा
เคे เคจिเคฏเคฎों เคा เคชाเคฒเคจ เคจเคนीं เคिเคฏा เคนो เคเคฐ เคฏเคน เค
เคชเคตाเคฆ เคเคค्เคชเคจ्เคจ เคเคฐเคจे เคा เคाเคฐเคฃ เคนो เคธเคเคคा เคนै, เคเคฌเคि เค
เคชเคตाเคฆ เคฌिเคจा เคिเคธी เคธिंเคैเค्เคธ เคค्เคฐुเคि เคे เคญी เคนो เคธเคเคคा เคนै เคเคฆाเคนเคฐเคฃ เคे เคฒिเค เค
เคชเคตाเคฆ เคैเคธे เคเคธी เคिเคธी เคซ़ाเคเคฒ เคोเคฒเคจे เคा เคช्เคฐเคฏाเคธ เคเคฐเคจा เคो เค
เคธ्เคคिเคค्เคต เคฎें เคจเคนीं เคนै, เคถूเคจ्เคฏ เคธे เคตिเคญाเคเคจ เคเคฆि।
เคเคธ เคช्เคฐเคाเคฐ เคช्เคฐเคค्เคฏेเค เคธिंเคैเค्เคธ เคค्เคฐुเคि เคเค เค
เคชเคตाเคฆ เคนै เคฒेเคिเคจ เคช्เคฐเคค्เคฏेเค เค
เคชเคตाเคฆ เคธिंเคैเค्เคธ เคค्เคฐुเคि เคจเคนीं เคนो เคธเคเคคा เคนै।)
Question2.
When are the following built-in exceptions raised? Give examples
to support your answers.
a) ImportError
ImportError :- It is raised when the requested module definition
is not found or If we are trying to import submodule from the module. (เคฏเคน เคคเคฌ เคเค ाเคฏा เคाเคคा เคนै เคเคฌ เค
เคจुเคฐोเคงिเคค เคฎॉเคก्เคฏूเคฒ เคชเคฐिเคญाเคทा เคจเคนीं เคฎिเคฒเคคी เคนै เคฏा เคฏเคฆि เคนเคฎ เคฎॉเคก्เคฏूเคฒ เคธे เคธเคฌเคฎॉเคก्เคฏूเคฒ เคเคฏाเคค เคเคฐเคจे เคा เคช्เคฐเคฏाเคธ เคเคฐ เคฐเคนे เคนोเคคे เคนैं।)
Program
:-
import sys
try:
from exception import myexception
except Exception as e:
print e
print sys.exc_type
Output
:- No module named exception <type
'exceptions.ImportError'>
b) IOError
IOError:- It stands for INPUT/ OUTPUT error. It is raised when I/O
operator fails such as file specified in a program statement cannot be opened
while print statement or the open() function. Example,
File not found, disk full.
(เคฏเคน เคเคจเคชुเค/เคเคเคเคชुเค เคค्เคฐुเคि เคे เคฒिเค เคนै। เคเคธे เคคเคฌ เคเค ाเคฏा เคाเคคा เคนै เคเคฌ I/O เคเคชเคฐेเคเคฐ เคตिเคซเคฒ เคนो เคाเคคा เคนै เคैเคธे เคช्เคฐोเค्เคฐाเคฎ เคธ्เคेเคเคฎेंเค เคฎें เคจिเคฐ्เคฆिเคท्เค เคซ़ाเคเคฒ เคो เคช्เคฐिंเค เคธ्เคेเคเคฎेंเค เคฏा เคเคชเคจ () เคซ़ंเค्เคถเคจ เคे เคฆौเคฐाเคจ เคจเคนीं เคोเคฒा เคा เคธเคเคคा เคนै। เคเคฆाเคนเคฐเคฃ, File not found, disk full.)
Program
:-
import sys
def hello():
try:
f = open ( "abc.txt", 'r' )
except IOError, e:
print e print sys.exc_type hello()
Output :-
[Errno 2] No such
file or directory: 'foo.txt'
<type
'exceptions.IOError'>
c) NameError
NameError :- A nameerror encounter when a local or global
variable name is not defined or found in the local or global scope o when you
used a function that wasn’t defined anywhere in your program. For example, If
you try to print a variable that hasn't been defined, you'll get this error.
(เคเคฌ เคนเคฎ เคिเคธी เคเคธे เคซ़ंเค्เคถเคจ เคा เคเคชเคฏोเค เคเคฐเคคे เคนैं เคो เคนเคฎाเคฐे เคช्เคฐोเค्เคฐाเคฎ เคฎें เคเคนीं เคญी เคชเคฐिเคญाเคทिเคค เคจเคนीं เคिเคฏा เคเคฏा เคฅा, เคคเคฌ เคเค เคธ्เคฅाเคจीเคฏ เคฏा เคตैเคถ्เคตिเค เคเคฐ เคจाเคฎ เคชเคฐिเคญाเคทिเคค เคจเคนीं เคिเคฏा เคเคฏा เคฏा เคธ्เคฅाเคจीเคฏ เคฏा เคตैเคถ्เคตिเค เคฆाเคฏเคฐे เคฎें เคชाเคฏा เคเคฏा เคฅा। เคเคฆाเคนเคฐเคฃ เคे เคฒिเค, เคฏเคฆि เคเคช เคเค เคตेเคฐिเคเคฌเคฒ เคो เคช्เคฐिंเค เคเคฐเคจे เคा เคช्เคฐเคฏाเคธ เคเคฐเคคे เคนैं เคिเคธे เคชเคฐिเคญाเคทिเคค เคจเคนीं เคिเคฏा เคเคฏा เคนै, เคคो เคเคชเคो เคฏเคน เคค्เคฐुเคि เคฎिเคฒेเคी।)
Exapmle :-
print (var +40)
Output :-
Traceback (most recent call last):
File "<string>", line
1, in <module>
NameError: name 'var' is not defined
d) ZeroDivisionError
ZeroDivisionError :- It is
raised when the denominator in a division operation is zero. (เคเคธे เคคเคฌ เคเค ाเคฏा เคाเคคा เคนै เคเคฌ เคเค เคกिเคตीเคเคจ เคเคชเคฐेเคถเคจ เคฎें เคนเคฐ เคถूเคจ्เคฏ เคนोเคคा เคนै।)
Example :- x = 0 y = 0 z = x/y
print ( z)
Output :-
Traceback (most recent call last):
File "<string>", line
3, in <module>
ZeroDivisionError: division by zero
Question3.
What is
the use of a raise statement? Write a code to accept two numbers and display
the quotient. Appropriate exception should be raised if the user enters the
second number (denominator) as zero (0).
(เคฐेเค เคธ्เคेเคเคฎेंเค เคा เค्เคฏा เคเคชเคฏोเค เคนै? เคฆो เคจंเคฌเคฐ เคธ्เคตीเคाเคฐ เคเคฐเคจे เคे เคฒिเค เคเค เคोเคก เคฒिเคें เคเคฐ เคญाเคเคซเคฒ เคช्เคฐเคฆเคฐ्เคถिเคค เคเคฐें। เคฏเคฆि เคเคชเคฏोเคเคเคฐ्เคคा เคฆूเคธเคฐी เคธंเค्เคฏा เคฎें (เคนเคฐ) เคถूเคจ्เคฏ เคे เคฐूเคช เคฎें (0) เคช्เคฐเคตेเคถ เคเคฐเคคा เคนै เคคो เคเคिเคค เค
เคชเคตाเคฆ เคเค ाเคฏा เคाเคจा เคाเคนिเค)
Ans:-
CODE :-
try:
a = int(input("Enter first
number: "))
b = int(input("Enter second
number: "))
quotient=(a/b)
if b == 0:
raise ValueError("can't be
zero")
except ValueError:
print("division by zero
error")
else:
print (quotient)
Output:-
Enter first number: 2
Enter second number: 0
Traceback (most recent call last):
File "<string>", line
4, in <module>
ZeroDivisionError: division by zero
Question4.
Use assert statement in Question No. 3 to test the division
expression in the program. (Program เคฎें
division
expression
เคชเคฐीเค्เคทเคฃ เคเคฐเคจे
เคे เคฒिเค
เคช्เคฐเคถ्เคจ เคธंเค्เคฏा 3 เคฎें assert
statement
เคा เคช्เคฐเคฏोเค เคเคฐें ।)
Ans:-
Code:-
num1 =
int(input("Enter first number: "))
num2 =
int(input("Enter second number: "))
assert
num2 != 0, "Invalid Operation"
print(num1
/num2)
Output:-
Enter
first number: 20
Enter
second number: 0
Traceback
(most recent call last):
File
"<string>", line 3, in <module>
AssertionError:
Invalid Operation
Question5.
Define the
following: a) Exception Handling b) Throwing an exception c) Catching an
exception
(เคจिเคฎ्เคจเคฒिเคिเคค เคो เคชเคฐिเคญाเคทिเคค เคเคฐें: เค) เค
เคชเคตाเคฆ เคนैंเคกเคฒिंเค เคฌी) เค
เคชเคตाเคฆ เคซेंเคเคจा เคธी) เค
เคชเคตाเคฆ เคชเคเคก़เคจा)
Ans:
a) Exception Handling
Sometimes while executing a program, the program does not execute
at all or the program executes but generates unexpected output or behaves
abnormally so these exception needs to be handled in order to achieve expected
output And this mechanism is known as exception handling.
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
When an exception occurs in the program it means exception is
raised or thrown and these exception need to be handled or caught. So for that
we write the code to handle those exception. And the code written to handle it
is known as exception handler. And this whole process to achieve normal flow of
program is known as exception handling.
(เคเคญी-เคเคญी
เคिเคธी เคช्เคฐोเค्เคฐाเคฎ เคो เคจिเคท्เคชाเคฆिเคค เคเคฐเคคे เคธเคฎเคฏ, เคช्เคฐोเค्เคฐाเคฎ เคฌिเคฒ्เคुเคฒ เคจिเคท्เคชाเคฆिเคค เคจเคนीं เคนोเคคा เคนै เคฏा เคช्เคฐोเค्เคฐाเคฎ เคจिเคท्เคชाเคฆिเคค เคนोเคคा เคนै เคฒेเคिเคจ เค
เคช्เคฐเคค्เคฏाเคถिเคค เคเคเคเคชुเค เคเคค्เคชเคจ्เคจ เคเคฐเคคा เคนै เคฏा เค
เคธाเคฎाเคจ्เคฏ เคฐूเคช เคธे เคต्เคฏเคตเคนाเคฐ เคเคฐเคคा เคนै เคเคธเคฒिเค เค
เคชेเค्เคทिเคค เคเคเคเคชुเค เคช्เคฐाเคช्เคค เคเคฐเคจे เคे เคฒिเค เคเคจ เค
เคชเคตाเคฆों เคो เคธंเคญाเคฒเคจे เคी เคเคตเคถ्เคฏเคเคคा เคนोเคคी เคนै เคเคฐ เคเคธ เคคंเคค्เคฐ เคो เค
เคชเคตाเคฆ เคนैंเคกเคฒिंเค เคे เคฐूเคช เคฎें เคाเคจा เคाเคคा เคนै।
เค
เคชเคตाเคฆ เคนैंเคกเคฒिंเค เคฐเคจเคाเคเคฎ เคค्เคฐुเคिเคฏों เคैเคธे ClassNotFoundException, IOException,
SQLException, RemoteException, เคเคฆि เคो เคธंเคญाเคฒเคจे เคे เคฒिเค เคเค เคคंเคค्เคฐ เคนै।
เคเคฌ เคช्เคฐोเค्เคฐाเคฎ เคฎें เคोเค เค
เคชเคตाเคฆ เคนोเคคा เคนै เคคो เคเคธเคा เคฎเคคเคฒเคฌ เคนै เคि เค
เคชเคตाเคฆ เคเค ाเคฏा เคฏा เคซेंเคा เคเคฏा เคนै เคเคฐ เคเคจ เค
เคชเคตाเคฆों เคो เคธंเคญाเคฒเคจे เคฏा เคชเคเคก़เคจे เคी เคเคตเคถ्เคฏเคเคคा เคนै। เคคो เคเคธเคे เคฒिเค เคนเคฎ เคเคจ เค
เคชเคตाเคฆों เคो เคธंเคญाเคฒเคจे เคे เคฒिเค เคोเคก เคฒिเคเคคे เคนैं। เคเคฐ เคเคธे เคนैंเคกเคฒ เคเคฐเคจे เคे เคฒिเค เคฒिเคे เคเค เคोเคก เคो เคเค्เคธेเคช्เคถเคจ เคนैंเคกเคฒเคฐ เคे เคจाเคฎ เคธे เคाเคจा เคाเคคा เคนै। เคเคฐ เคाเคฐ्เคฏเค्เคฐเคฎ เคे เคธाเคฎाเคจ्เคฏ เคช्เคฐเคตाเคน เคो เคช्เคฐाเคช्เคค เคเคฐเคจे เคी เคเคธ เคชूเคฐी เคช्เคฐเค्เคฐिเคฏा เคो เค
เคชเคตाเคฆ เคช्เคฐเคฌंเคงเคจ เคे เคฐूเคช เคฎें เคाเคจा เคाเคคा เคนै।)
b) Throwing an exception
When an error
occurs, interpreter creates an object called the exception object. This object
contains information about the error, such as its type, file name and where it
occurred in the program. Then this object is handed over to the runtime system
so that it can find an appropriate code to handle this particular exception.
This process of creating an exception object and handing it over to the runtime
system is called throwing an exception.
The raise
statement
is used to throw an exception explicitly.
raise statement
of python allows you to throw an exception at any time or explicitly.
Raise statement
:- The raise statement can be used to throw an exception manually . You can
specify what kind of error to raise, the error may be a built-in exception or
may be a user-defined one.
Syntax :- raise
exception-name[(optional argument)]
- The argument
is generally a string that is displayed when the exception is raised.
เคเคฌ เคोเค
เคค्เคฐुเคि เคนोเคคी
เคนै, เคคो
เคฆुเคญाเคทिเคฏा เคเค
เคเคฌ्เคेเค्เค เคฌเคจाเคคा
เคนै เคिเคธे
เค
เคชเคตाเคฆ เคเคฌ्เคेเค्เค เคเคนा เคाเคคा เคนै। เคเคธ เคเคฌ्เคेเค्เค เคฎें เคค्เคฐुเคि เคे เคฌाเคฐे เคฎें เคाเคจเคाเคฐी เคนोเคคी เคนै, เคैเคธे เคि เคเคธเคा เคช्เคฐเคाเคฐ, เคซ़ाเคเคฒ เคा เคจाเคฎ เคเคฐ เคช्เคฐोเค्เคฐाเคฎ เคฎें เคฏเคน เคเคนां เคนुเค। เคซिเคฐ เคเคธ เคเคฌ्เคेเค्เค เคो เคฐเคจเคाเคเคฎ เคธिเคธ्เคเคฎ เคो เคธौंเคช เคฆिเคฏा เคाเคคा เคนै เคคाเคि เคฏเคน เคเคธ เคตिเคถेเคท เค
เคชเคตाเคฆ เคो เคธंเคญाเคฒเคจे เคे เคฒिเค เคเคชเคฏुเค्เคค เคोเคก เคขूंเคข เคธเคे। เคเค เค
เคชเคตाเคฆ เคตเคธ्เคคु เคฌเคจाเคจे เคเคฐ เคเคธे เคฐเคจเคाเคเคฎ เคธिเคธ्เคเคฎ เคो เคธौंเคชเคจे เคी เคเคธ เคช्เคฐเค्เคฐिเคฏा เคो เค
เคชเคตाเคฆ เคซेंเคเคจा เคเคนा เคाเคคा เคนै।
เค
เคชเคตाเคฆ เคो เคธ्เคชเคท्เค เคฐूเคช เคธे เคซेंเคเคจे เคे เคฒिเค เคฐेเค़ เคธ्เคेเคเคฎेंเค เคा เคเคชเคฏोเค เคिเคฏा เคाเคคा เคนै।
Python
เคा Raise statement เคเคชเคो
เคिเคธी เคญी
เคธเคฎเคฏ เคฏा
เคธ्เคชเคท्เค เคฐूเคช
เคธे เค
เคชเคตाเคฆ
throw
เคเคฐเคจे เคी
เค
เคจुเคฎเคคि เคฆेเคคा
เคนै।
Raise statement: - Raise statement เคा
เคเคชเคฏोเค เค
เคชเคตाเคฆ
เคो เคฎैเคจ्เคฏुเค
เคฒ เคฐूเคช เคธे throw เคเคฐเคจे
เคे เคฒिเค
เคिเคฏा เคा
เคธเคเคคा เคนै।
เคเคช เคจिเคฐ्เคฆिเคท्เค เคเคฐ เคธเคเคคे เคนैं เคि เคिเคธ เคช्เคฐเคाเคฐ เคी เคค्เคฐुเคि เคो เคเค ाเคจा เคนै, เคค्เคฐुเคि เคเค เค
ंเคคเคฐ्เคจिเคนिเคค เค
เคชเคตाเคฆ เคนो เคธเคเคคी เคนै เคฏा เคเคชเคฏोเคเคเคฐ्เคคा เคฆ्เคตाเคฐा เคชเคฐिเคญाเคทिเคค เคเค เคนो เคธเคเคคी เคนै।
เคธिंเคैเค्เคธ: - Syntax :- raise
exception-name[(optional argument)]
Optional Argument เคเคฎ เคคौเคฐ เคชเคฐ เคเค เคธ्เค्เคฐिंเค เคนै เคो เค
เคชเคตाเคฆ เคเค ाเค เคाเคจे เคชเคฐ เคช्เคฐเคฆเคฐ्เคถिเคค เคนोเคคा เคนै।
c) Catching an exception
The runtime system searches the entire program for a block of
code, called the exception handler that can handle the raised exception. It
searches for the method in which the error has occurred and the exception has
been raised. If not found, then it searches the method from which this method
(in which exception was raised) was called. This continues till the exception
handler is found. This entire list of methods is known as call stack. When a
suitable handler is found in the call stack, it is executed by the runtime
process. This process of executing a suitable handler is known as catching the
exception.
(เคฐเคจเคाเคเคฎ เคธिเคธ्เคเคฎ เคोเคก เคे เคเค เคฌ्เคฒॉเค เคे เคฒिเค เคชूเคฐे เคช्เคฐोเค्เคฐाเคฎ เคฎें เคोเค เคเคฐเคคा เคนै, เคिเคธे เค
เคชเคตाเคฆ เคนैंเคกเคฒเคฐ เคเคนा เคाเคคा เคนै เคो เคเค ाเค เคเค เค
เคชเคตाเคฆ เคो เคธंเคญाเคฒ เคธเคเคคा เคนै। เคฏเคน เคเคธ method เคी
เคोเค เคเคฐเคคा
เคนै เคिเคธเคฎें เคค्เคฐुเคि เคนुเค เคนै เคเคฐ เค
เคชเคตाเคฆ เคเค ाเคฏा เคเคฏा เคนै। เคฏเคฆि method เคจเคนीं เคฎिเคฒเคคा เคนै, เคคो เคฏเคน เคเคธ method เคी เคोเค เคเคฐเคคा เคนै เคिเคธเคธे เคฏเคน method (เคिเคธเคฎें เค
เคชเคตाเคฆ เคเค ाเคฏा เคเคฏा เคฅा) เคเคนा เคเคฏा เคฅा। เคฏเคน เคคเคฌ เคคเค เคाเคฐी เคฐเคนเคคा เคนै เคเคฌ เคคเค เค
เคชเคตाเคฆ เคนैंเคกเคฒเคฐ เคจเคนीं เคฎिเคฒ เคाเคคा। methods เคी
เคเคธ เคชूเคฐी
เคธूเคी เคो
เคॉเคฒ เคธ्เคैเค
เคे เคฐूเคช
เคฎें เคाเคจा
เคाเคคा เคนै।
เคเคฌ เคॉเคฒ
เคธ्เคैเค เคฎें
เคเค เคเคชเคฏुเค्เคค เคนैंเคกเคฒเคฐ เคฎिเคฒเคคा เคนै, เคคो เคเคธे เคฐเคจเคाเคเคฎ เคช्เคฐเค्เคฐिเคฏा เคฆ्เคตाเคฐा เคจिเคท्เคชाเคฆिเคค เคिเคฏा เคाเคคा เคนै। เคเค เคเคชเคฏुเค्เคค เคนैंเคกเคฒเคฐ เคो เคจिเคท्เคชाเคฆिเคค เคเคฐเคจे เคी เคเคธ เคช्เคฐเค्เคฐिเคฏा เคो เค
เคชเคตाเคฆ เคो เคชเคเคก़เคจे เคे เคฐूเคช เคฎें เคाเคจा เคाเคคा เคนै।)
Question6.
เค
เคชเคตाเคฆों เคो เคชเคเคก़เคจे เคी เคต्เคฏाเค्เคฏा เคเคฐें -try เคเคฐ except เคฌ्เคฒॉเค เคा เคช्เคฐเคฏोเค เคเคฐเคคे เคนुเค |
Ans:
In order to
achieve the normal flow of program , raised exception should be caught and
handled. And these can be done using using a try block and except block.
- Try block :-
While writing or debugging a program, a user might doubt an exception to
occur in a particular part of the code. Such suspicious lines of codes are
put inside a try block.
- Except block
:- The code that handles the exceptions is written in the except clause.
We can use multiple except block for a single try block to catch more than
one type of error.
เคाเคฐ्เคฏเค्เคฐเคฎ เคे เคธाเคฎाเคจ्เคฏ เคช्เคฐเคตाเคน เคो เคช्เคฐाเคช्เคค เคเคฐเคจे เคे เคฒिเค, เคเค ाเค เคเค เค
เคชเคตाเคฆ เคो เคชเคเคก़ा เคเคฐ เคธंเคญाเคฒा เคाเคจा เคाเคนिเค। เคเคฐ เคฏเคน try block เคเคฐ except block เคा เคเคชเคฏोเค เคเคฐเคे เคिเคฏा เคा เคธเคเคคा เคนै।
try block: เคช्เคฐोเค्เคฐाเคฎ เคฒिเคเคคे เคฏा เคกिเคฌเค เคเคฐเคคे เคธเคฎเคฏ, เคเคชเคฏोเคเคเคฐ्เคคा เคो เคोเคก เคे เคिเคธी เคตिเคถेเคท เคญाเค เคฎें เคนोเคจे เคตाเคฒे เค
เคชเคตाเคฆ เคชเคฐ เคธंเคฆेเคน เคนो เคธเคเคคा เคนै। เคोเคก เคी เคเคธ เคคเคฐเคน เคी เคธंเคฆिเค्เคง เคชंเค्เคคिเคฏों เคो เคเค try เคฌ्เคฒॉเค
เคे เค
ंเคฆเคฐ
เคฐเคा เคाเคคा
เคนै।
except block: - เค
เคชเคตाเคฆों เคो เคธंเคญाเคฒเคจे เคตाเคฒा เคोเคก เค्เคฒॉเค เคो เคोเคก़เคเคฐ เคฒिเคा เคนोเคคा เคนै। เคนเคฎ เคเค เคธे เค
เคงिเค เคช्เคฐเคाเคฐ เคी เคค्เคฐुเคि เคो เคชเคเคก़เคจे เคे เคฒिเค เคเคเคฒ เคช्เคฐเคฏाเคธ เคฌ्เคฒॉเค เคे เคฒिเค เคฌ्เคฒॉเค เคो เคोเคก़เคเคฐ เคเคाเคงिเค เคा เคเคชเคฏोเค เคเคฐ เคธเคเคคे เคนैं।
Syntax :-
try:
[program statements where exceptions might
occur]
except
[exception-name]:
[code for
exception handling if the exception-name error is encountered]
except
[exception-name]:
[ code for
exception handling if the exception-name error is encountered]
Program :-
try:
numerator=50
denom=int(input("Enter the
denominator"))
quotient=(numerator/denom)
print ("Division performed
successfully")
except ZeroDivisionError:
print ("Denominator as ZERO....
not allowed")
print(“OUTSIDE try..except block”)
Output:
Enter the
denominator 0
0
Denominator as
ZERO.... not allowed
Question7.
Consider the code
given below and fill in the blanks.
print ("
Learning Exceptions...")
try:
num1= int(input
(" Enter the first number")
num2=int(input("Enter
the second number"))
quotient=(num1/num2)
print ("Both
the numbers entered were correct")
except
_____________: # to enter only integers
print ("
Please enter only numbers")
except
____________: # Denominator should not be zero
print("
Number 2 should not be zero")
else:
print("
Great .. you are a good programmer")
___________: # to
be executed at the end
print(" JOB
OVER... GO GET SOME REST")
Ans:
print ("
Learning Exceptions...")
try:
num1= int(input
(" Enter the first number"))
num2=int(input("Enter
the second number"))
quotient=(num1/num2)
print ("Both
the numbers entered were correct")
except ValueError
: # to enter only integers
print ("
Please enter only numbers")
except
ZeroDivisionError : # Denominator should not be zero
print("
Number 2 should not be zero")
else:
print("
Great .. you are a good programmer")
finally
: # to be
executed at the end
print(" JOB
OVER... GO GET SOME REST")
Question8.
You have learnt how to use math module in Class XI. Write a code
where you use the wrong number of arguments for a method (say sqrt() or pow()).
Use the exception handling process to catch the ValueError exception.
Ans:
Code: import math
x =
int(input('Enter a positive number:\n'))
try:
print(f'Square
Root of {x} is {math.sqrt(x).}')
except ValueError
as ve:
print(f'You have
entered {x}, which is not a positive number.')
Output:
Enter a positive
number:
16
Square Root of 16
is 4.0.
Enter a positive
number:
-10
You have entered
-10, which is not a positive number.
Comments
Post a Comment