
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
try and except in Python
To use exception handling in python, we first need to catch the all except clauses.
Python provides, “try” and “except” keywords to catch exceptions. The “try” block code will be executed statement by statement. However, if an exception occurs, the remaining “try” code will not be executed and the except clause will be executed.
try: some_statements_here except: exception_handling
Let’s see above syntax with a very simple example −
try: print("Hello, World!") except: print("This is an error message!")
Output
Hello, World!
Above is a very simple example, let’s understand the above concept with another example −
import sys List = ['abc', 0, 2, 4] for item in List: try: print("The List Item is", item) r = 1/int(item) break except: print("Oops!",sys.exc_info()[0],"occured.") print('\n') print("Next Item from the List is: ") print() print("The reciprocal of",item,"is",r)
Output
The List Item is abc Oops! <class 'ValueError'> occured. Next Item from the List is: The List Item is 0 Oops! <class 'ZeroDivisionError'> occured. Next Item from the List is: The List Item is 2 The reciprocal of 2 is 0.5
In the above program, the loops run until we get (as user input) an integer that has a valid reciprocal. The code which causes an exception to raise is placed within the try block.
In case some exception occurs, it will be caught by the except block. We can test the above program with different exception errors. Below are some of the common exception errors −
IOError
Raised in case we cannot open the file.
ImportError
Raised in case module is missing.
ValueError
It happened whenever we pass the argument with the correct type but an inappropriate value of a built-in operator or function.
KeyboardInterrupt
Whenever the user hits the interrupt key (generally control-c)
EOFError
Exception raised when the built-in functions hit an end-of-file condition (EOF) without reading any data.
- Related Articles
- try and except in Python Program
- Explain try, except and finally statements in Python.
- Explain Try, Except and Else statement in Python.
- How can I write a try/except block that catches all Python exceptions?
- The try-finally Clause in Python
- Python - Remove all characters except letters and numbers
- How to raise an exception in one except block and catch it in a later except block in Python?
- Product of Array Except Self in Python
- What is the difference between 'except Exception as e' and 'except Exception, e' in Python?
- Try, catch, throw and throws in Java
- How to remove characters except digits from string in Python?
- try keyword in C#
- How to match anything except space and new line using Python regular expression?
- How to use the ‘except’ clause with multiple exceptions in Python?
- How to use the ‘except clause’ with No Exceptions in Python?
