0% found this document useful (0 votes)
47 views3 pages

Exception Handling Note_2025-26

The document explains the differences between syntax errors and exceptions in Python, highlighting that syntax errors prevent code execution until corrected, while exceptions occur during execution due to unexpected conditions. It details various built-in exceptions such as SyntaxError, ValueError, and ZeroDivisionError, along with their explanations. Additionally, it covers how to handle exceptions using try-except blocks and the role of the finally clause in ensuring certain code executes regardless of exceptions.
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)
47 views3 pages

Exception Handling Note_2025-26

The document explains the differences between syntax errors and exceptions in Python, highlighting that syntax errors prevent code execution until corrected, while exceptions occur during execution due to unexpected conditions. It details various built-in exceptions such as SyntaxError, ValueError, and ZeroDivisionError, along with their explanations. Additionally, it covers how to handle exceptions using try-except blocks and the role of the finally clause in ensuring certain code executes regardless of exceptions.
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
You are on page 1/ 3

Chapter-Exception Handling in Python

1. How is a Syntax Error different from an Exception?


Ans. Syntax errors are detected when we have not followed the rules of the particular programming Language while
writing a program. These errors are Also known as parsing errors. On encountering a Syntax error, the interpreter does
not execute the Program unless we rectify the errors, save and Rerun the program. When a syntax error is encountered
While working in shell mode, python displays the name of the error and a small description about the error.

when a syntax error is encountered while running a program in script mode

An exception is defined as an unexpected condition in a program that causes the program’s flow to be
interrupted.When the Python interpreter attempts to execute invalid code, it throws an exception, and if
the exception is not handled, it disturbs the regular flow of the program’s instructions and outputs a
traceback. Exceptions are a form of error in which the code has the correct syntax but contains a fault.
There are many different types of exceptions, but some of the most prevalent are: ImportError,
ZeroDivisionError, NameError, and TypeError.
2.Explain the different types of built-in exceptions.
Ans Built-in Exceptions Commonly occurring exceptions are usually defined in the compiler/interpreter.
These are called built-in exceptions. Python’s standard library is an extensive collection of built-in
exceptions that deals with the commonly occurring errors (exceptions) by providing the standardized
solutions for such errors. On the occurrence of any built-in exception, the appropriate exception handler
code is executed which displays the reason along with the raised exception name. The programmer then
has to take appropriate action to handle it.
Name of Built-in- Explanation
Exceptions
SyntaxError It is raised when there is an error in the syntax of the Python code
ValueError It is raised when a built-in method or operation receives an argument
that has the right data type but mismatched or inappropriate values
IOError It is raised when the file specified in a program statement cannot be opened.
KeyboardInterrupt It is raised when the user accidentally hits the Delete or Esc key
while executing a program due to which the normal flow of the
program is interrupted

ImportError It is raised when the requested module definition is not found


EOFError It is raised when the end of file condition is reached without reading any data by
input().
ZeroDivisionError It is raised when the denominator in a division operation is zero
IndexError It is raised when the index or subscript in a sequence is out of range
NameError It is raised when a local or global variable name is not defined.
IndentationError It is raised due to incorrect indentation in the program code.
TypeError It is raised when an operator is supplied with a value of incorrect data type
OverFlowError It is raised when the result of a calculation exceeds the maximum limit for
numeric data type.
Example->

3.How are exceptions handled? Give examples to support your answer .


Ans.Each and every exception has to be handled by the programmer to avoid the program from crashing
abruptly. This is done by 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
 Exception handlers separate the main logic of the program from the error detection and correction
code. The segment of code where there is any possibility of error or exception, is placed inside one
block. The code to be executed in case the exception has occurred, is placed inside another block. These
statements for detection and reporting the exception do not affect the main logic of the program.
 The interpreter keeps track of the exact position where the error has occurred.
Exceptions can be handled by:
try: ………… except: ……….. Finally
Using try..except block
try:
a=int(input('Enter value1:'))
b=int(input('Enter value2:'))
print(a/b)
except ZeroDivisionError:
print('Denominator should not be zero')
Use of multiple except clauses
try:
a=int(input('Enter value1:'))
b=int(input('Enter value2:'))
print(a/b)
except ZeroDivisionError:
print('Denominator should not be zero')
except ValueError:
print('Only integers should be entered')
Use of except without specifying an exception
try:
a=int(input('Enter value1:'))
b=int(input('Enter value2:'))
print(a/b)
except ValueError:
print('Only integers should be entered')
except:
print(" OOPS.....SOME EXCEPTION RAISED")
4.What is the use of the finally clause? Give examples to support your answer.
Ans The try statement in Python can also have an optional finally clause. The statements inside the finally
block are always executed regardless of whether an exception has occurred in the try block or not. It is a
common practice to use finally clause while working with files to ensure that the file object is closed. If
used, finally should always be placed at the end of try clause, after all except blocks
Use of finally clause
try:
a=int(input('Enter value1:'))
b=int(input('Enter value2:'))
print(a/b)
except ZeroDivisionError:
print('Denominator should not be zero')
except ValueError:
print('Only integers should be entered')
finally:
print('Its over now')

You might also like