When can I use a forward declaration in C/C++?

Ravi Ranjan
Updated on 02-Jun-2025 18:20:45

913 Views

A forward declaration informs the compiler that a class, function, or variable is declared earlier, but it will be defined later in the code. In this article, our task is to understand the forward declaration and when to use it. When is Forward Declaration Used in C/C++? The forward declaration can be used in C/C++ in the following cases: In C++, it is used for declaring a friend function. Using the forward declaration, we can reduce the header files that we include in the code. When ... Read More

How to check whether a string ends with one from a list of suffixes in Python?

Rajendra Dharmkar
Updated on 02-Jun-2025 17:30:08

1K+ Views

A suffix is a group of letters added at the end of a word. In Python, we can check if a string ends with any one of multiple suffixes using the endswith() method. It takes a tuple of suffixes as an argument and returns True if the string ends with any of them. This is useful for checking file extensions, URL endings, or word patterns. Using endswith() with Multiple Suffixes The endswith() method allows you to check if a string ends with any one of several suffixes by passing them as a tuple. This helps to check for multiple ... Read More

How to handle invalid arguments with argparse in Python?

Rajendra Dharmkar
Updated on 02-Jun-2025 17:29:49

2K+ Views

Argparse is a Python module that helps you create easy-to-use command-line interfaces. When building these interfaces, it is important to handle invalid arguments properly to give clear feedback to users and prevent your program from crashing unexpectedly. There are several ways to handle invalid arguments in argparse. You can catch errors using try-except blocks, restrict allowed options with choices, validate inputs with custom functions and error messages, or control the number of arguments using nargs. Using these methods makes your command-line programs more reliable and user-friendly. Using try and except Blocks One simple method to handle invalid arguments is ... Read More

How to catch a thread's exception in the caller thread in Python?

Sarika Singh
Updated on 02-Jun-2025 15:39:16

911 Views

Python threads do not automatically pass exceptions to the caller thread. To catch thread exceptions in the main thread, you can use custom thread classes, the ThreadPoolExecutor with futures, or a shared queue. Using a Wrapper with threading.Thread The easiest way to catch exceptions in a thread is to wrap the target function in a try-except block. You can then store the exception in a shared variable or object so it can be checked later from the main thread. Example In this example, we create a custom thread class that stores any exceptions in an instance variable. After the ... Read More

How to rethrow Python exception with new type?

Sarika Singh
Updated on 02-Jun-2025 15:37:30

284 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

How to handle Python exception in Threads?

Sarika Singh
Updated on 02-Jun-2025 15:35:09

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

How to catch OSError Exception in Python?

Sarika Singh
Updated on 02-Jun-2025 15:30:21

904 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

How to handle python exception inside if statement?

Sarika Singh
Updated on 02-Jun-2025 15:28:41

3K+ Views

You cannot directly use an if statement to catch exceptions in Python. Instead, use a try-except block and place if conditions inside the except to respond differently based on the type or message of the exception. This allows you to write conditional logic for exception handling. Using if Inside the except Block You can write if conditions inside the except block to check the type or details of the exception and take appropriate action. Example: Matching Exception Message with "if" In this example, we catch a ValueError and use if statement to inspect its message - def process_input(value): ... Read More

How to catch NotImplementedError Exception in Python?

Sarika Singh
Updated on 02-Jun-2025 15:25:44

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

How to catch SystemExit Exception in Python?

Sarika Singh
Updated on 02-Jun-2025 15:22:21

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 due to a call ... Read More

Advertisements