
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Found 10398 Articles for Python

515 Views
In Python, the EnvironmentError occurs when errors related to the system's environment, such as file I/O issues or hardware failures, occur. In the latest Python versions (i.e., 3.3+), this exception is the same as OSError. In this article, you will learn how to catch EnvironmentError (or OSError) to handle system-level errors and prevent programs from crashing abruptly. Some of the common reasons to catch EnvironmentError are - When a file operation fails When a directory cannot be accessed or found When a hardware or device-related issue occurs Using try-except to Catch EnvironmentError To catch EnvironmentError, use a try-except ... Read More

680 Views
A TypeError occurs in Python when we perform an operation on an object of an inappropriate type. For example, adding a string to an integer or calling a non-callable object. In this article, you will learn how to catch and handle TypeError exceptions using different methods in Python. TypeError is raised when you use the wrong data type in an operation. You can handle this exception using try-except blocks to prevent the program from crashing and help you correct input. Always test your code where type mismatches are possible, especially when working with user input or dynamic data. There are ... Read More

1K+ Views
Python uses indentation to define blocks of code instead of curly braces or keywords. If the indentation is not correct, Python raises an IndentationError. In this article, you will learn how to catch and handle IndentationError in Python using various approaches. An IndentationError in Python occurs when the indentation rules are not followed properly. It is raised during the parsing stage, not during execution. Therefore, to catch it, you must execute the code dynamically as a string. Using exec(), compile(), and custom wrapper functions, you can catch and handle this error in Python programs. We can use the following methods ... Read More

2K+ Views
SyntaxError in Python occurs when the interpreter encounters invalid syntax, such as missing colons, unmatched parentheses, incorrect indentation, or invalid keywords. Since this error happens during the code compilation stage (before execution), it cannot be caught using a regular try-except block. To handle it, you must wrap the faulty code inside the exec() or compile() functions within a try-except block. Here, we are demonstarting the occurence of SyntaxError and handling it using the following methods - exec() Method compile() Method Using a custom function with exception handling ... Read More

887 Views
EOFError is commonly seen when a program tries to read input, but there is no data left to read. This can happen, for example, when input is redirected from a file or when the user provides no input and presses Ctrl+D (Unix) or Ctrl+Z (Windows). The best way to catch EOFError is by using the try-except block. Below are various ways to handle EOFError properly in Python - Using try-except block with EOFError Using try-except-else block Using try-except-finally block Using try-except Block with EOFError In Python, ... Read More

648 Views
Whenever Python comes across a variable or name that is not defined in the local or global namespace, it raises a NameError. This helps in debugging and ensures that variables are properly declared before using it. There are various ways to catch and handle a NameError in Python. The most common method is using a try-except block. Below are different approaches to catch a NameError exception - Using try-except block with NameError Using try-except-else block Using try-except-finally block Using try-except Block with NameError In Python, you ... Read More

4K+ Views
In Python, an IndexError occurs when you try to access a position (index) in a list, tuple, or similar collection that isn't there (does not exist). It means your program is trying to access the elements that are not available in the sequence (object). Using try-except to Catch IndexError You can use a try-except block to catch (handle) an IndexError and stop your program from crashing if you try to access an index that doesn't exist (or invalid). Example In this example, we try to access the 5th index of a list that only has 3 elements, which causes an ... Read More

2K+ Views
When an arithmetic operation exceeds the limits of the variable type, an OverflowError is raised. Long integers allocate more space as values grow, so they end up raising MemoryError. Floating point exception handling is not standardized, however. Regular integers are converted to long values as needed. Using try-except to Catch OverflowError You can use a try-except block to catch an OverflowError and prevent your program from crashing when a calculation overflows. Example: Catching an OverflowError In this example, we calculate a very large exponent which can cause an OverflowError on some systems, and catch it - try: ... Read More

1K+ Views
While executing the statements that perform arithmetic operations, if any operation results in an illegal value, an arithmetic exception occurs (run time).In Python, ArithmeticError represents this exception, and it is the base class for all errors that occur during arithmetic operations, such as division by zero, overflow, or floating-point errors. Catching this exception helps to manage errors that arise from calculations. Catching ArithmeticError with try-except Block You can use a try-except block in Python to catch ArithmeticError and handle errors related to arithmetic operations like division by zero or overflow. This allows your program to continue running smoothly even if ... Read More

8K+ Views
In Python, an IOError (or OSError in latest versions) occurs when an input/output operation fails. For example, when we are trying to read a file that doesn’t exist, writing to a file that is read-only, or accessing a corrupted device. You can catch IOError using a try-except block to handle file input/output errors in Python. For compatibility with Python 3, we need to use OSError or catch both using a tuple. Using try-except Block You can catch IOError using a try-except block. This helps you handle file-related errors without crashing the program. Example In this example, we try to open ... Read More