
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Handle Exception Thrown by Except Clause in Python
In Python, sometimes an except block itself may raise an exception. Handling such exceptions properly is important to make sure that your program does not crash unexpectedly and to maintain clean error handling.
Exceptions raised inside an except block can be handled by nesting try-except blocks within it.
Exceptions Inside except Blocks
An except block is meant to handle errors, but it can also raise exceptions if the code inside it causes errors. You need to handle these secondary exceptions to avoid program termination.
Example
In this example, the except block tries to divide by zero, which raises a new exception while handling the original one -
try: num = int("abc") except ValueError: print("Handling ValueError...") x = 1 / 0 # This raises ZeroDivisionError inside except
The output shows the original exception is handled but then a new exception is raised causing the program to crash -
Handling ValueError... Traceback (most recent call last): ... ZeroDivisionError: division by zero
Using Nested try-except Inside except Block
To handle exceptions that occur inside an except block, you can nest another try-except inside it. This catches secondary exceptions and prevents the program from crashing.
Example
In this example, the inner try-except catches the new exception raised inside the first except block -
try: num = int("abc") except ValueError: print("Handling ValueError...") try: x = 1 / 0 except ZeroDivisionError: print("Handled ZeroDivisionError inside except block.")
The output shows both exceptions are handled gracefully -
Handling ValueError... Handled ZeroDivisionError inside except block.
Using Multiple except Blocks
You can use multiple except blocks to handle different types of exceptions separately, allowing your code to manage errors more clearly.
Example: Separate handling for different exceptions
In this example, the outer except handles ValueError, and the inner one handles the possible ZeroDivisionError inside it -
try: num = int("abc") except ValueError: print("Handling ValueError...") try: x = 1 / 0 except ZeroDivisionError as e: print(f"Caught inside except: {e}")
The output clearly separates the two errors -
Handling ValueError... Caught inside except: division by zero
Catching all Secondary Exceptions
If you want to catch any unexpected errors inside the except block, you can use a generic except Exception clause to catch all exceptions.
Example
In this example, the inner except catches any exception that might occur inside the first except block -
try: num = int("abc") except ValueError: print("Handling ValueError...") try: x = 1 / 0 except Exception as e: print("Caught exception inside except block:", e)
The output shows the generic catch working -
Handling ValueError... Caught exception inside except block: division by zero