Sarika Singh

Sarika Singh

142 Articles Published

Articles by Sarika Singh

Page 9 of 15

How to handle Python exception in Threads?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 2K+ 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 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 ...

Read More

How to catch NotImplementedError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 2K+ 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 ...

Read More

How to catch ImportError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 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. 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 Module installation issues or missing dependencies Catching ImportError with Try-Except Like any other exception, we can catch the ImportError exception using try-except blocks. Here are various scenarios where ImportError ...

Read More

How to catch SystemExit Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 3K+ 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 ...

Read More

How to catch StopIteration Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 2K+ Views

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__() method 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? You can retrieve the contents of an Iterator object using the Python built-in function next() or the __next__() method. The next() function internally calls the __next__() method. These methods return the next available ...

Read More

How to catch FloatingPointError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 2K+ Views

A FloatingPointError 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 operations like dividing by zero; instead, it returns inf or nan. To catch this error, you need to enable it explicitly using NumPy. When Does FloatingPointError Occur? FloatingPointError can occur in cases like: Divide by zero in floating-point calculations (if enabled) Overflow in floating-point operations Invalid operations resulting in nan values Underflow when numbers become too small to represent Enabling FloatingPointError in NumPy To raise FloatingPointError, you ...

Read More

How to catch ZeroDivisionError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 780 Views

The ZeroDivisionError is raised in Python when a number is divided by zero. Since dividing by zero is mathematically undefined, Python throws this error to prevent invalid operations. In this article, you will learn how to catch and handle ZeroDivisionError using the try-except block. By handling this exception, the execution of the program continues even after the ZeroDivisionError exception occurs. The ZeroDivisionError generally occurs in the following cases ? Dividing any number by zero using / or // operators Modulo operation with zero as the divisor using % Using try-except to Catch ZeroDivisionError We ...

Read More

How to catch ValueError using Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 515 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 ...

Read More

How to catch LookupError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 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 ...

Read More

How to catch EnvironmentError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 24-Mar-2026 705 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 ...

Read More
Showing 81–90 of 142 articles
« Prev 1 7 8 9 10 11 15 Next »
Advertisements