0% found this document useful (0 votes)
48 views

CLASS XII COMPUTER SCIENCE NOTES CHAPTER 1 Chapter 1 Exception Handling in Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

CLASS XII COMPUTER SCIENCE NOTES CHAPTER 1 Chapter 1 Exception Handling in Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

NOTES

CLASS 12
COMPUTER SCIENCE
Chapter 1: Exception Handling in Python
Introduction
Exception handling in Python is a mechanism to handle runtime errors, ensuring
that the normal flow of the program is maintained. Errors in a program can be
broadly classified into three categories:
1. Syntax Errors: These are mistakes in the use of the Python language.
2. Runtime Errors: Errors that occur during the execution of the program.
3. Logical Errors: Errors that occur due to logical mistakes in the program.
Types of Errors and Exceptions
Syntax Errors
Syntax errors are detected during the parsing of the code. If the parser
encounters an incorrect statement, it raises a syntax error.
Example:
python
Copy code
print("Hello world"
The above code will raise a syntax error because the parenthesis is not closed.
Runtime Errors
Runtime errors occur during the execution of a program and are also known as
exceptions. These errors occur due to illegal operations.
Example:
python
Copy code
print(1/0)
The above code will raise a ZeroDivisionError because division by zero is not
allowed.
Logical Errors
Logical errors occur when the code runs without crashing, but it produces an
incorrect result.
Example:
python
Copy code
result = 2 + 2
print("The result is", result) # This will print "The result is 4" which is correct
logically
Exception Handling
Python provides a way to handle these runtime errors using try, except, else, and
finally blocks.
Try and Except Block
The try block contains the code that might throw an exception. The except block
contains the code that runs if an exception occurs.
Example:
python
Copy code
try:
print(1/0)
except ZeroDivisionError:
print("You cannot divide by zero!")
In this example, the ZeroDivisionError is handled, and the message "You cannot
divide by zero!" is printed.
Else Clause
The else block is executed if the code in the try block does not raise an exception.
Example:
python
Copy code
try:
result = 10 / 2
except ZeroDivisionError:
print("You cannot divide by zero!")
else:
print("The result is", result)
Here, since no exception is raised, the else block is executed, printing "The result
is 5.0".
Finally Clause
The finally block is executed regardless of whether an exception occurs or not. It
is typically used for cleanup actions.
Example:
python
Copy code
try:
result = 10 / 0
except ZeroDivisionError:
print("You cannot divide by zero!")
finally:
print("This will always be executed.")
Even though a ZeroDivisionError is raised, the finally block is still executed,
printing "This will always be executed."
Handling Multiple Exceptions
Python allows handling multiple exceptions using a single try block.
Example:
python
Copy code
try:
result = 10 / 0
except ZeroDivisionError:
print("You cannot divide by zero!")
except ValueError:
print("Invalid input!")
Here, the code handles both ZeroDivisionError and ValueError.
Raising Exceptions
You can raise exceptions using the raise keyword.
Example:
python
Copy code
try:
raise ValueError("An error occurred")
except ValueError as e:
print(e)
This code raises a ValueError with a custom error message "An error occurred".
Custom Exceptions
Python allows creating custom exceptions by deriving classes from the built-in
Exception class.
Example:
python
Copy code
class CustomError(Exception):
pass
try:
raise CustomError("This is a custom error")
except CustomError as e:
print(e)
Here, CustomError is a user-defined exception, which is raised and handled.
Conclusion
Exception handling is a crucial aspect of programming, ensuring that runtime
errors do not disrupt the normal flow of the program. By using try, except, else,
and finally blocks, Python provides a robust way to handle errors and exceptions
gracefully.

You might also like