Comprehensive_Exception_Handling_Notes_Class12
Comprehensive_Exception_Handling_Notes_Class12
termination.
- Division by zero
1. Syntax Errors:
- Occur when Python cannot interpret the code due to incorrect syntax.
- Example:
- Example:
try:
except ExceptionType:
else:
finally:
Detailed Explanation
Example Code
try:
result = 10 / num
print("Result:", result)
except ZeroDivisionError:
except ValueError:
else:
finally:
print("Execution complete.")
Raising Exceptions
Example:
if age < 0:
Example:
try:
result = 10 / num
Custom Exceptions
You can define your own exception classes by inheriting from the Exception
class.
Example:
class NegativeValueError(Exception):
pass
if num < 0:
- Use try for risky code and except for handling exceptions.
code.