
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Explain Try, Except and Else statement in Python.
The common method to handle exceptions in python is using the "try-except" block. We can even include an else clause after except clause. The statements in the else block are executed if there is no exception in the try statement.
The optional else clause is executed if and when control flows off the end of the try clause except in the case of an exception or the execution of a return, continue, or break statement.
Example
The given code can be rewritten as follows
a = [11, 8, 9, 2] try: foo = a[3] except: print "index out of range" else: print "index well within range"
Output
This gives the output
index well within range
- Related Articles
- Explain try, except and finally statements in Python.
- try and except in Python
- try and except in Python Program
- Explain if-else statement in C language
- Explain Nested if-else statement in C language
- Explain else-if ladder statement in C language
- IF ELSE statement in a MySQL Statement?
- Using else conditional statement with for loop in python
- How to indent an if...else statement in Python?
- How to use else statement with Loops in Python?
- Java if-else statement
- What is basic syntax of Python if...else statement?
- How can I write a try/except block that catches all Python exceptions?
- How to use else conditional statement with for loop in python?
- What is the ‘if...else if...else’ statement in Java and how to use it?

Advertisements