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

228 Views
In Python, you can catch an exception and raise a new one, while keeping the original exception for more details. This helps when you want to change a low-level error into a clearer, higher-level error that fits your application better. Sometimes, catching a specific exception and raising a new one helps you to handle problems or give a clearer message. Python lets you do this using the raise NewException from OriginalException syntax. Rethrowing with a New Exception Type You can rethrow an exception using the from keyword to keep the original error details and traceback. This helps you provide ... Read More

1K+ Views
Python allows you to run multiple tasks at the same time using threads. However, handling exceptions in threads can be tricky because exceptions that happen inside a thread don't get passed to the main thread by default. To deal with this, you can catch errors inside the thread's function or create a custom thread class to handle them. This helps you to find issues in threads and handle them properly in the main program. Exception Handling in Threads When an exception occurs inside a thread, it only affects that thread, and unless explicitly handled, it will be ignored silently. ... Read More

824 Views
The OSError in Python is commonly encountered when a system-related error occurs, such as a file not found, permission denied, or disk I/O errors. It is one of the built-in exceptions that helps in handling operating system-level failures. In this article, you will learn how to catch and handle OSError using try-except blocks with examples. When Does OSError Occur? OSError may occur in the following situations - Trying to open a non-existent file. Permission denied while accessing a file or directory. Invalid file path or I/O operation failures. Example: File Not Found In the following example, we try ... Read More

1K+ Views
The NotImplementedError exception in Python is raised when an abstract method or operation that should be implemented by a subclass is not implemented. It is commonly used as a placeholder in base classes to indicate that subclasses are expected to override the method. In this article, you will learn how to catch and handle the NotImplementedError in Python using simple examples. When Does NotImplementedError Occur? The NotImplementedError is usually raised in the following cases - A method is defined in a base class but is not implemented and used as a placeholder for child classes to override. The subclass ... Read More

4K+ Views
The ImportError exception in Python is raised when the interpreter cannot find or load a module that is being imported using the import statement. Catching the ImportError ExceptionLike any other exception, we can catch the ImportError exception using the try-except blocks. In this article, we discuss various scenarios where the ImportError exception occurs, with examples, and catch the generated exception using the try-except blocks in each scenario.When Does ImportError Occur?The ImportError generally occurs in the following cases -Trying to import a module that doesn't exist.Misspelling the module name.Trying to import a function or class that is not available in the specified module.Example: Importing a ... Read More

2K+ Views
The SystemExit exception in Python is raised when the sys.exit() function is called. It is used to exit the program cleanly. Although this exception is not an error in the traditional sense, it can be caught using a try-except block if needed, especially when you want to prevent a script from terminating abruptly. This article explains how SystemExit works and how you can catch and handle it in Python programs. When Does SystemExit Occur? The SystemExit exception is raised when - You call sys.exit() function to exit the program. The Python interpreter is terminating due to a call ... Read More

1K+ Views
StopIteration Exception in Python The StopIteration exception in Python is raised to indicate that there are no more items left in an iterator to retrieve. It occurs when a call to the next() or, __next__() reaches the end of an iterable. In this article, you will learn how and when StopIteration is raised, and how to catch and handle it using try-except blocks. When Does StopIteration Occur? When can retrieve the contents of an Iterator object, using the Python built-in function next() or the __next__() method (of the iterator object). The next() function internally calls the _next__() method. These methods return the ... Read More

481 Views
StandardError in Python 2In Python 2, StandardError was a built-in exception class that is a base class for all built-in exceptions except for SystemExit, KeyboardInterrupt, and GeneratorExit. Using this class, we were able to catch the most common runtime errors in a single except block. However, since Python 3, the StandardError class has been deprecated, and now all built-in exceptions directly inherit from the Exception class. If you are using Python 3, you should catch exceptions using Exception instead of StandardError. Example Following is a basic example of raising the StandardError exception in Python 2 - try: value ... Read More

2K+ Views
Catching FloatingPointError Exception in PythonFloatingPointError in Python is an exception that occurs when there is an error in floating-point calculations. By default, Python does not raise this error for basic operations like dividing by zero; instead, it returns inf or nan. To catch this error, you need to enable it explicitly using the numpy module. In this article, you will learn how to catch a FloatingPointError by enabling it through NumPy settings and handling it using a try-except block. When Does FloatingPointError Occur? FloatingPointError can occur in cases like - Divide by zero in floating-point calculations (if enabled) ... Read More

528 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