Computer Science · Class 12 Optional
Ch 1Exception Handling in Python — Class 12 Computer Science, concept-first.
When you write a Python program, you expect it to run smoothly and give the correct output. But sometimes, things go wrong. The program might not run at all, or it might run but produce unexpected results, or it might behave in a strange, abnormal way.
9
Q&A
4
Concepts
40m
Section weightage
Computational Thinking and Programming – 2 · across all 7 lessons
Key concepts
Hover a concept to preview it and jump to its most relevant Q&A.
Exception Handling Hierarchy
Think of a large organisation. When a small problem occurs on the shop floor — say a printer runs out of paper — the person working there handles it immediately.
Most relevant Q&A
- "Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.Free
- When are the following built-in exceptions raised? Give examples to support your answers. a) ImportError b) IOError c) NameError d) ZeroDivi…Free
- Define the following: a) Exception Handling b) Throwing an exception c) Catching an exceptionPreview
- Explain catching exceptions using try and except block.Preview
Chapter contents
The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.
Introduction
When you write a Python program, you expect it to run smoothly and give the correct output. But sometimes, things go wrong.
Syntax Errors
A syntax error is what happens when you break the grammar rules of Python while writing code. The interpreter cannot understand what you have written because it does not follow the language's structur…
Exceptions
An exception is not a syntax mistake. It is an error that happens during the execution of a program, even when the code is perfectly correct in terms of grammar and spelling.
Built-in Exceptions
Built-in exceptions are the errors that the Python interpreter already knows about. When you write code, certain mistakes happen so often that the language designers have pre-defined a specific except…
Raising Exceptions
When a program runs into an error, the Python interpreter automatically raises (or throws) an exception. This is the normal way Python signals that something has gone wrong.
The raise Statement
The raise statement is the tool Python gives you to deliberately throw an exception. You are not waiting for an error to happen — you are actively creating one when your code detects a condition that…
The assert Statement
The assert statement is a debugging aid that tests a condition inside your program. Think of it as a sanity check: you are asserting that something must be true at that point in the code.
Handling Exceptions
Exception handling is the process of writing additional code in a program to deal with exceptions — those runtime errors that would otherwise crash the program abruptly.
Need for Exception Handling
Exception handling is not unique to Python — it is a technique used across most modern programming languages, including C++ and Java.
Process of Handling Exception
When Python encounters an error during program execution, it does not simply crash. Instead, it creates a special object called the exception object.
Catching Exceptions
An exception is said to be caught when a piece of code specifically designed to handle that exception actually runs.
try...except...else clause
The try...except block can be extended with an optional else clause. This clause is placed after all the except blocks and before the end of the try statement.
Finally Clause
The try statement can also have an optional finally clause. Statements written inside the finally block always get executed, regardless of whether an exception occurred in the try block or not, and re…
Recovering and continuing with finally clause
If an error is detected in the try block and the exception raised there is handled by a matching except clause, that except block runs as usual.
Summary
- Syntax errors (parsing errors) are detected when the rules of the programming language have not been followed while writing a program.
Exercises
+−Show 9 questionsHide questions9 questions
- Q1"Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.Free
- Q2When are the following built-in exceptions raised? Give examples to support your answers. a) ImportError b) IOError c) NameError d) ZeroDivi…Free
- Q3What is the use of a raise statement? Write a code to accept two numbers and display the quotient. Appropriate exception should be raised if…Free
- Q4Use assert statement in Question No. 3 to test the division expression in the program.Preview
- Q5Define the following: a) Exception Handling b) Throwing an exception c) Catching an exceptionPreview
- Q6Explain catching exceptions using try and except block.Preview
- Q7Consider the code given below and fill in the blanks. ```python print(" Learning Exceptions...") try: num1 = int(input("Enter the first numb…Preview
- Q8You 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…Preview
- Q9What is the use of finally clause? Use finally clause in the problem given in Question No. 7.Preview