CBSE Computer Science Class 12 Past Paper PDF 2023-24
Document Details
Uploaded by Deleted User
2023
CBSE
Tags
Summary
This document is a CBSE Computer Science past paper for Class 12, covering the topic of exception handling in Python. It explains different types of errors, including compile-time and run-time errors, and provides practical examples. The PDF includes questions on exception handling concepts and procedures in Python.
Full Transcript
Computer Science As Per CBSE Syllabus 2023-24 KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Types of Errors: Types of errors : (i) Compile-time errors. These are the errors resulting out of violation...
Computer Science As Per CBSE Syllabus 2023-24 KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Types of Errors: Types of errors : (i) Compile-time errors. These are the errors resulting out of violation of programming language’s grammar rules. All syntax errors are reported during compilation. (ii) Run-time errors. The errors that occur during runtime because of unexpected situations. Such errors are handled through exception handling routines of Python. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Syntax Error Examples: These are the errors resulting out of violation of programming language’s grammar rules. Note: All syntax errors are reported during compilation. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE What is an Exception : Exceptions are unexpected events or errors that occur during the execution of a program, such as a division by zero or accessing an invalid memory location. These events can lead to program termination or incorrect results. “It is an exceptional event that occurs during runtime and causes normal program flow to be disrupted.” Some common examples of Exceptions are : Divide by zero errors Accessing the elements of an array beyond its range Invalid input m Hard disk crash Opening a non-existent file Heap memory exhausted KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Exception (Examples): Note: Observe that there is nothing wrong with the program syntax, it is only when we try to divide an integer with zero , an exception is generated. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Exception (Examples): Note: Only When we are trying to access a list element with an non existing index an exception is generated. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Exception (Examples): Note: Only When we are trying to convert a string to integer the Exception generated. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE What is Exception Handling? Exception handling in Python is a mechanism used to handle runtime errors that occur during the execution of a Python program Exception handling allows a program to gracefully handle such exceptions and recover from errors by taking corrective actions instead of terminating abruptly. In Python, exception handling is implemented using a try-except block KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Exception Handling using. try and except Block: The try and except block in Python is a way to handle exceptions or errors that may occur during code execution. This mechanism prevents the program from crashing by allowing it to continue running even if an error is encountered. In a try block, you write the code that might raise an exception. If an exception occurs, the code execution jumps to the corresponding except block, where you can handle the error or take alternative actions. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Exception (Examples): KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Example: Write a program to ensure that an integer is entered as input and in case any other value is entered, it displays a message – ‘Not a valid integer’ KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE General Built-in Python Exceptions: Exception Name Description Raised when one of the built-in functions (input( )) hits an end-of-file condition (EOF) without reading any data. (NOTE. the file.read( ) and EOFError file.readline( ) methods return an empty string when they hit EOF.) Raised when an I/O operation (such as a print statement, the built-in open( ) function or a method of a file object) fails for an I/O-related reason, e.g., IO Error “file not found” or “disk full”. Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the NameError name that could not be found. Raised when a sequence subscript is out of range, e.g., from a list of length 4 if you try to read a value of index like 8 or E8 etc. (Slice indices are silently IndexError truncated to fall in the allowed range ; if an index is not a plain integer, TypeError is raised.) ImportError Raised when an import statement fails to find the module definition or when a from... import fails to find a name that is to be imported. Raised when an operation or function is applied to an object of inappropriate type, e.g., if you try to compute a square-root of a string value. The TypeError associated value is a string giving details about the type mismatch. Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described ValueError by a more precise exception such as IndexError. ZeroDivisionError Raised when the second argument of a division or modulo operation is zero. OverflowError Raised when the result of an arithmetic operation is too large to be represented. KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys ImportError Raised when the module given with import statement is not found. KeyboardInterrupt Raised when keys Esc, Del or Ctrl+C is pressed during program execution and normal program flow gets disturbed. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Second Argument of the Exception Block: We can also provide a second argument (optional) for the except block, which gives a reference to the exception object. try: #code Except as : #code for error handling here KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Handling Multiple Errors Handling multiple exceptions in Python allows a single try-except block to handle different types of exceptions using multiple except blocks. This allows a program to handle various types of errors that may occur during runtime and take corrective measures accordingly. In a try-except block, each except block is associated with a specific exception type, and the block containing the code to handle that exception is executed if the corresponding exception occurs in the try block. By handling multiple exceptions, programmers can write more robust and less error-prone code. Syntax: KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Example: Program to handle multiple exceptions: Note (Execution Order): The is executed first ; if, during the course of executing the , an exception is raised that is not handled otherwise, and the is executed, with bound to the exception, if found ; if no matching except suite is found then unnamed except suite is executed. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE finally Block : The finally block is a part of the try-except block in Python that contains the code that is executed regardless of whether an exception is raised or not. The syntax of the try-except-finally block is as follows: the try block contains the code that may raise an exception. If an exception occurs, the control is transferred to the corresponding except block, which contains the code to handle the exception. The finally block contains the code that is executed after the try-except blocks, regardless of whether an exception occurred or not. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Example : Program using finally block KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Example : What is the order of execution? In this example, if the user enters an invalid input or attempts to divide by zero, the corresponding except block handles the exception and prints an error message to the user. If no exception occurs, the else block is executed and prints the result. Finally, the finally block is executed and prints a message to indicate the completion of the program execution KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Practice Programs Example : 1. Write a Python program that takes two numbers as input from the user and calculates the quotient of the two numbers. Handle the exceptions that may occur during the program execution, such as invalid input or division by zero. 2. Write a Python program that reads a file and displays its contents on the screen. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading error. 3. Write a Python program that takes a list of integers as input from the user and calculates the average of the numbers. Handle the exceptions that may occur during the program execution, such as invalid input or division by zero. 4. Write a Python program that reads a CSV file and displays its contents on the screen. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading error. 5. Write a Python program that takes a string as input from the user and converts it to an integer. Handle the exceptions that may occur during the program execution, such as invalid input or string conversion error. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE Practice Programs Example : 6. Write a Python program that takes a list of numbers as input from the user and finds the maximum and minimum numbers in the list. Handle the exceptions that may occur during the program execution, such as invalid input or empty list error. 7. Write a Python program that reads a file and writes its contents to another file. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading/writing error. 8. Write a Python program that takes two strings as input from the user and concatenates them. Handle the exceptions that may occur during the program execution, such as invalid input or string concatenation error. 9. Write a Python program that reads a text file and counts the number of words in it. Handle the exceptions that may occur during the program execution, such as the file not found error or file reading error. 10. Write a Python program that takes a string as input from the user and reverses it. Handle the exceptions that may occur during the program execution, such as invalid input or string reversal error. KV Coders Visit www.kvcoders.in more regular updates LET’S CRACK CBSE COMUPTER SCIENCE