Advanced
Exception Handling
Learn to handle runtime errors gracefully using try, except, and related constructs to make your code more robust.
Exception Handling in Python
Exception handling allows you to manage errors gracefully without stopping your program abruptly. Python provides structured syntax to catch and handle exceptions.
Syntax
Use try
and except
blocks to catch exceptions:
pythonCopyEdittry: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
Catching Multiple Exceptions
You can catch multiple specific exceptions by listing them in a tuple:
pythonCopyEdittry: # code that might raise exceptions x = int("abc") except (ValueError, TypeError): print("Invalid input or type error occurred.")
Using else
and finally
else
runs if no exceptions occur.finally
always runs, regardless of exceptions, often used for cleanup.
pythonCopyEdittry: file = open("data.txt") except FileNotFoundError: print("File not found.") else: print("File opened successfully.") file.close() finally: print("Execution finished.")
Raising Exceptions
You can raise your own exceptions using raise
:
pythonCopyEditdef divide(a, b): if b == 0: raise ValueError("Cannot divide by zero!") return a / b
Exception Handling in Python
Exception handling allows you to manage errors gracefully without stopping your program abruptly. Python provides structured syntax to catch and handle exceptions.
Syntax
Use try
and except
blocks to catch exceptions:
pythonCopyEdittry: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
Catching Multiple Exceptions
You can catch multiple specific exceptions by listing them in a tuple:
pythonCopyEdittry: # code that might raise exceptions x = int("abc") except (ValueError, TypeError): print("Invalid input or type error occurred.")
Using else
and finally
else
runs if no exceptions occur.finally
always runs, regardless of exceptions, often used for cleanup.
pythonCopyEdittry: file = open("data.txt") except FileNotFoundError: print("File not found.") else: print("File opened successfully.") file.close() finally: print("Execution finished.")
Raising Exceptions
You can raise your own exceptions using raise
:
pythonCopyEditdef divide(a, b): if b == 0: raise ValueError("Cannot divide by zero!") return a / b
Exception Handling in Python
Exception handling allows you to manage errors gracefully without stopping your program abruptly. Python provides structured syntax to catch and handle exceptions.
Syntax
Use try
and except
blocks to catch exceptions:
pythonCopyEdittry: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
Catching Multiple Exceptions
You can catch multiple specific exceptions by listing them in a tuple:
pythonCopyEdittry: # code that might raise exceptions x = int("abc") except (ValueError, TypeError): print("Invalid input or type error occurred.")
Using else
and finally
else
runs if no exceptions occur.finally
always runs, regardless of exceptions, often used for cleanup.
pythonCopyEdittry: file = open("data.txt") except FileNotFoundError: print("File not found.") else: print("File opened successfully.") file.close() finally: print("Execution finished.")
Raising Exceptions
You can raise your own exceptions using raise
:
pythonCopyEditdef divide(a, b): if b == 0: raise ValueError("Cannot divide by zero!") return a / b