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

Unlock Your Coding Potential: Learn Python Online Today!