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

Chapter 2-Exception Handling (1)

The document discusses exception handling in programming, outlining two types of errors: compile-time errors and run-time errors. It explains how to handle exceptions using try-except-finally blocks in Python, providing an example of handling a ZeroDivisionError. The advantages include improved reliability and cleaner code, while the disadvantages mention performance overhead and increased complexity.

Uploaded by

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

Chapter 2-Exception Handling (1)

The document discusses exception handling in programming, outlining two types of errors: compile-time errors and run-time errors. It explains how to handle exceptions using try-except-finally blocks in Python, providing an example of handling a ZeroDivisionError. The advantages include improved reliability and cleaner code, while the disadvantages mention performance overhead and increased complexity.

Uploaded by

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

Exception Handling:

Types of Error:

(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.

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.

Handling exceptions using try-exceptfinally blocks

try:
# Some Code....which may have runtime error
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)

Example:

try:
k = 9//0 # raises divide by zero exception.
print(k)

# handles zerodivision exception


except ZeroDivisionError:
print("Can't divide by zero")

else:
print(“No error”)

finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')

Advantages of Exception Handling:


• Improved program reliability
• Simplified error handling
• Cleaner code
• Easier debugging
Disadvantages of Exception Handling:
• Performance overhead
• Increased code complexity
• Possible security risks

You might also like