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.

  1. 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.
  2. 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

Popular posts from this blog

เค…เคชเคจे เค•ंเคช्เคฏूเคŸเคฐ เคฎें เคชाเค‡เคฅเคจ เค•ैเคธे เค‡เคจเคธ्เคŸॉเคฒ เค•เคฐें How to install Python in your Computer

เคชाเค‡เคฅเคจ เคธीเค–ें เคนिंเคฆी เคฎें Python Introduction In Hindi RBSE Python Classes

Rajasthan Sanganak Previous Question Papers