
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- Java if-else statement
- Java if-then-else statement
- How to indent an if...else statement in Python?
- How to use else statement with Loops in Python?
- Using else conditional statement with for loop in python
- What is basic syntax of Python if...else statement?
- Explain try and catch statements in JavaScript with examples.
- Java if-else-if ladder statement
Advertisements