How to Catch Multiple Exceptions in Python Definition, Syntax, Examples, and Best Practices Made by Rachit Joshi 1. Introduction Exception handling in Python helps a program respond to errors that occur during execution. Examples include entering text where a number is expected, dividing by zero, or trying to open a file that does not exist. Without a matching handler, an unhandled exception can stop normal program execution. Multiple exception handling means preparing a program to respond to more than one kind of exception. Python supports separate except clauses, a tuple of exception types in one clause, and handlers for common parent classes. 2. Errors and Exceptions An exception is an event raised during program execution when an operation cannot be completed normally. A traceback provides details about where the exception occurred. A bug is a defect in program logic; not every bug necessarily produces an exception. print(10 / "One") # TypeError: an integer cannot be divided by a string The expression above raises TypeError. Handling an expected exception can help the program display a clear message instead of showing an unhandled traceback to the user. 3. Basic try - except Syntax try: # Code that may raise an exception risky_operation() except SomeException: # Handle that specific exception handle_problem() Python executes the try block first. If an exception occurs, Python searches for a matching except clause. If the try block succeeds, the except clauses are skipped. 4. Handling Different Exceptions Separately Separate except clauses are useful when different errors need different messages or recovery actions. try: first = float(input("Enter first number: ")) second = float(input("Enter second number: ")) if second == 0: raise ZeroDivisionError result = first / second print("Result:", result) except ValueError: print("Please enter valid numbers.") except ZeroDivisionError: print("You cannot divide by zero.") except Exception as error: print("Unexpected error:", error) Explanation: • ValueError is raised when text cannot be converted to a float. • ZeroDivisionError is handled when the divisor is zero. • The general Exception handler is a fallback for unexpected ordinary exceptions; use it thoughtfully. 5. Catching Multiple Exceptions in One except Clause When multiple exception types need the same response, place them in a tuple. This reduces repeated code. try: first = float(input("Enter first number: ")) second = float(input("Enter second number: ")) print(f"{first} divided by {second} is {first / second}") except (ValueError, ZeroDivisionError): print("Please enter valid numbers and do not divide by zero.") The tuple tells Python to run the same handler for either ValueError or ZeroDivisionError. Use this pattern only when a shared response makes sense. 6. Identifying the Exception That Was Caught Use as error to bind the exception object to a variable. The variable can be used to inspect the exception type or display a diagnostic message. try: first = float(input("Enter first number: ")) second = float(input("Enter second number: ")) print(first / second) except (ValueError, ZeroDivisionError) as error: print(f"A {type(error).__name__} occurred: {error}") The as error part is important: without it, the name error in the handler would not be defined by this except clause. 7. Handling Related Exceptions with a Superclass Some exception classes share a parent class. For example, FileNotFoundError and PermissionError are subclasses of OSError. Catching OSError can handle a range of file - system errors when the same response is suitable. try: with open("datafile.txt", "r", encoding="utf - 8") as file: print(file.readlines()) except OSError as error: print(f"File operation failed: {error}") A specific handler is usually preferable when the program can recover differently from different failures. Avoid catching BaseException for ordinary error handling because it also includes special control - flow exceptions. 8. Ignoring Specific Exceptions with contextlib.suppress() Sometimes a specific exception is expected and safe to ignore. Python's contextlib.suppress() can express that intention clearly. from contextlib import suppress with suppress(FileNotFoundError): with open("optional.txt", "r", encoding="utf - 8") as file: print(file.read()) This example ignores FileNotFoundError if the optional file is missing. Other exception types still propagate. Do not suppress errors unless ignoring them is intentional and safe. 9. Exception Groups and except* (Python 3.11+) ExceptionGroup can represent several exceptions together. The except* syntax handles matching members of an exception group and is useful for certain concurrent or batch operations. try: raise ExceptionGroup( "Several operations failed", [ValueError("invalid value"), TypeError("wrong type")] ) except* ValueError as group: print("Handled ValueError group:", group) except* TypeError as group: print("Handled TypeError group:", group) This feature is different from a normal tuple in an except clause: except* is designed for exception groups. For everyday code that raises one exception at a time, regular except clauses are usually enough. 10. Common Mistakes to Avoid • Do not use a bare except clause unless there is a specific reason. • Do not write except (ValueError, ZeroDivisionError) as error and then forget to bind the variable. • Avoid using pass to silently ignore errors unless that is intentional. • Place specific exception handlers before broader parent - class handlers. • Keep the try block focused on operations that are expected to fail. • Do not use a broad Exception handler to hide programming mistakes or continue with invalid state. 11. Quick Comparison Approach When to use it Example Separate except clauses Different errors need different actions except ValueError: Exception tuple Several errors share one response except (ValueError, TypeError): Superclass handler Related errors share a safe response except OSError: suppress() A specific exception can safely be ignored suppress(FileNotFoundError) except* clauses Handle members of an ExceptionGroup except* ValueError: 1 2 . Conclusion Python provides flexible ways to handle multiple exceptions. Use separate except clauses when errors require different actions, a tuple when they share a response, and a common superclass when related errors can be handled together. Use contextlib.suppress() only for intentionally ignored exceptions, and use ExceptionGroup with except* when a program needs to handle grouped failures. Specific, readable exception handling improves reliability and makes debugging easier.