GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python Try Except - GeeksforGeeks Python Try ExceptLast Updated : 8 Jun, 2026 Errors and exceptions can interrupt the normal flow of a program during execution. Python provides try and except blocks to handle such errors gracefully without stopping the entire program. When an error occurs inside the try block, Python transfers control to the except block, allowing the program to handle the error safely.
Example: The below example handles a division by zero error using try and except.
Output An exception occurred. Explanation: The statement 10 / 0 raises a division by zero error inside the try block. Python stops executing the remaining code in try and moves to the except block, which prints the error message. Syntax
Parameters:
Working of try-except Blocks
Common Exception Errors
ExamplesExample 1: In this example, the code inside the try block runs successfully because no exception occurs. The except block is skipped.
Output Result: 5 Explanation: Since 10 // 2 is valid, the try block executes normally and prints the result. Example 2: This example shows how except handles an error when dividing by zero.
Output Cannot divide by zero Explanation: 10 // 0 raises a ZeroDivisionError, so Python jumps to the except ZeroDivisionError block. Example 3: This example uses Exception as e to display the actual error message generated by Python.
Output Error: unsupported operand type(s) for //: 'int' and 'str' Error: integer division or modulo by zero Explanation: The first call causes a type error because "2" is a string. The second call raises a division by zero error. Both are caught using Exception as e. Else Clauseelse block is used with try-except and executes only when no exception occurs in the try block. If an exception is raised, the else block is skipped. Syntax:
Example: In this example, the else block runs only when the division operation is successful.
Output Result: 5 Cannot divide by zero Explanation:
Finally Keywordfinally block is always executed after the try and except blocks, whether an exception occurs or not. It is commonly used for cleanup tasks such as closing files or releasing resources. Syntax:
Example: In this example, the finally block executes even though a division by zero error occurs.
Output Cannot divide by zero This block always executes Explanation: 10 // 0 raises a ZeroDivisionError, so the except block runs. After that, the finally block executes regardless of the exception. Nested Try-Except BlocksPython allows one try-except block to be placed inside another try-except block. This is called a nested try-except block and is useful when different sections of code may raise different exceptions. Example: In this example, the outer block handles division errors, while the inner block handles list index errors.
Output Result: 3 Invalid index access Execution completed --- Cannot divide by zero Execution completed Explanation:
Related Articles:Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |