
- 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
How to handle an exception in Python?
The simplest way to handle exceptions in python is using the "try-except" block.
Example
try: fob = open("test.txt", "r") fob.write("This is my test file for exception handling!!") except IOError: print "Error: can\'t find the file or read data" else: print "Write operation is performed successfully on the file" fob.close()
Output
Error: can't find the file or read data
- Related Articles
- How to handle an exception in JSP?
- How to handle Python exception in Threads?
- How to handle python exception inside if statement?
- How do you handle an exception thrown by an except clause in Python?
- How to handle an exception in JShell in Java 9?
- How to handle exception inside a Python for loop?
- How to handle a python exception within a loop?
- How to handle an exception using lambda expression in Java?\n
- How to use the try-finally clause to handle exception in Python?
- How to handle the Runtime Exception in Java?
- How to handle the exception using UncaughtExceptionHandler in Java?
- How to raise an exception in Python?
- What is the best way to handle list empty exception in Python?
- How can we create an exception filter to handle unhandled exceptions in C#\nASP.NET WebAPI?
- How to pass argument to an Exception in Python?

Advertisements