
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 10401 Articles for Python

544 Views
ZeroDivisionError Exception in Python The ZeroDivisionError is raised in Python when a number is divided by zero. Since dividing by zero is mathematically undefined, Python throws this error. In this article, you will learn how to catch and handle ZeroDivisionError using the try-except block. Usually, when an exception occurs, the program will be terminated abruptly, leaving the statements after the exception unexecuted. By handling this exception, the execution of the program continues even after the ZeroDivisionError exception (an invalid division attempt). The ZeroDivisionError generally occurs in the following cases - Dividing any number by zero using / ... Read More

345 Views
While passing arguments to a Python function, if the datatype of the given values is different from the function parameters, a TypeError is raised. But if the type of the argument is accurate and its value is inappropriate, a ValueError exception is raised In this article, you will learn how to catch ValueError exceptions using the general Exception class, which can catch all built-in exceptions, including ValueError. The ValueError exception commonly occurs when - You convert a string to an integer or float, but the string is not valid You pass an invalid value ... Read More

2K+ Views
LookupError in Python is the base class for errors that occur when a lookup using a key or index fails to find the expected value. This includes exceptions like IndexError and KeyError. If any lookup operation fails, a LookupError or one of its subclasses is raised. In this article, you will learn how to catch LookupError exceptions in Python and handle them to prevent your program from crashing abruptly. LookupError is commonly raised when - You access an invalid index in a list or a tuple. You use a missing key ... Read More

502 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

674 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

873 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

642 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