0% found this document useful (0 votes)
4 views4 pages

Ch 1-Exception Handling in Python

The document provides an overview of exception handling in Python, detailing syntax errors, raise and assert statements, and types of errors. It explains built-in exceptions, the need for exception handling, and the use of try-except blocks, including multiple except clauses, else, and finally clauses. Additionally, it defines key terms related to exceptions and outlines the process of handling exceptions.

Uploaded by

syedharmain127
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Ch 1-Exception Handling in Python

The document provides an overview of exception handling in Python, detailing syntax errors, raise and assert statements, and types of errors. It explains built-in exceptions, the need for exception handling, and the use of try-except blocks, including multiple except clauses, else, and finally clauses. Additionally, it defines key terms related to exceptions and outlines the process of handling exceptions.

Uploaded by

syedharmain127
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Presidency PU College, Kogilu, Yelahanka.

CHAPTER 1 : EXCEPTION HANDLING IN PYTHON


1. Write a note on syntax error.
 Syntax errors are detected when we have not followed the rules of
the programming language .
 These errors are also known as parsing errors or compile time
errors.
Eg : marks = 10
if marks > 20:
Print “GOOD SCORE”.
2. Write a note on raise statement.
The raise statement can be used to throw an exception.
Syntax:
raise exception_name[(optional argument)]
raise is a keyword; the argument is generally a string that is displayed
when the
exception is raised.
Example:
raise Exception("OOPS: An Exception has occurred")
3. Write a note on assert statement.
It is tests an expression. If the expression is False, it raises an
AssertionError.
This statement is generally used in the beginning of the function.
Syntax:
assert Expression[,arguments]
Example:
a=4
b=0
print("The value of a / b is : ")
assert (b != 0), "Zero Division Error"
print(a / b).

4. Explain the types of errors.


[Link] Name of the error Explanation
1 Syntax Error (or) Compile  When grammatical rules are
Time Error (or) Parsing not followed in syntax.
Error
2 Runtime Error(Exception)  It is detected during program
executes.
3 Logical Error  Program runs but gives
incorrect output.

5. What is exception handling? Mention any two keywords used in


exception handling.
Writing additional code in a program to give proper messages or
instructions to the user on encountering an exception. This process is
known as exception handling.
Keywords – try,except,else,finally.(any two)

1
Presidency PU College, Kogilu, Yelahanka.
6. Explain Built-in exceptions.
[Link] Name of the Explanation
exception
1 SyntaxError Raised when there is an error in the syntax
of the Python code.
2 ValueError Raised when a method receives correct data
type but incorrect or mismatched values.
3 IOError Raised when a specified file cannot be
opened.
4 KeyboardInterrupt Raised when the user presses keys (like Esc)
that interrupt program flow.
5 ImportError Raised when a module cannot be found.
6 ZeroDivisionError Raised when dividing number by zero.
7 EOFError Raised when input() reaches the end of a file
without reading data.
8 IndexError Raised when an index is out of range in a
sequence.
9 NameError Raised when a local variable or global
variable name is not defined.
10 IndentationError Raised due to incorrect indentation in the
code.
11 TypeError Raised when an operator gets a value of the
wrong data types.
12 OverflowError Raised when the result of a calculation
exceeds the maximum limit for numeric
types.

7. Explain the need for exception handling.


Essential to prevent program crashes by capturing and managing runtime
errors.
 Separates main program logic from error detection and correction
code.
 The compiler/interpreter tracks the exact error location.
 Applicable to both user-defined and built-in exceptions.

8. Explain try and except block.


 The try block encloses the code that might raise an exception.
 The except block specifies how to handle a particular exception.
Syntax:
try:
[statements exceptions might occur]
except [exception-name]:
[ code for exception handling]
Example:
try:
num = int(input("Enter a number: "))
result = 10 / num
print("Result:", result)
except ZeroDivisionError:
print("You cannot divide by zero!")

2
Presidency PU College, Kogilu, Yelahanka.

9. Explain multiple except clause.


You can use multiple except clauses to catch and handle different types of
exceptions in a single try block.

Example:
try:
num = int(input("Enter a number: "))
result = 10 / num
print("Result:", result)
except ZeroDivisionError:
print("You cannot divide by zero!")
except ValueError:
print("Please enter a valid number.")

10. Explain try...except…else clause.


Executes if no exception is raised
Example:
try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("You cannot divide by zero!")
else:
print("Result:", result)

11. Explain finally clause.


The finally block is always executed, regardless of whether an exception
occurred or not.
Example:
try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("You cannot divide by zero!")
else:
print("Result:", result)
finally:
print(“execution completed”)

12. Difference between raise and try-except blocks.


raise try-except
To throw an exception To catch and handle
exception.
Used to signal an error Used to handle errors
gracefully
Programs stops with error Program continues after
handling.

3
Presidency PU College, Kogilu, Yelahanka.

13. What is an exception object in python and what information


does it contain.
 When an error occurs, Python interpreter creates an object called the
exception object.
 It contains information about the error like its type, filename and
position in the program where the error has occurred
14. Write a flowchart for process of handling exception.

15. Define.
(a) Exception : Exceptions are errors that occur at runtime when the
program is being executed.
(b) Built-in-Exception : Commonly occurring exceptions are usually
defined in the compiler/interpreter. These are called built-in-
exceptions.
(c) Exception handling : Writing additional code in a program to give
proper messages or instructions to the user on encountering an
exception. This process is known as exception handling.
(d) Exception object : When an error occurs, Python interpreter
creates an object called the exception object.
(e) Throwing an exception : The process of creating an exception
object and handling it over to the runtime system is called throwing an
exception.
(f) Call stack : The hierarchical search in reverse order continuous till the
exception handler is found, this entire list of method is known as call
stack.
(g) Exception handler : The runtime system searches the entire
program for a block of code, called the exception handler .

You might also like