In Python, you can create your own custom exceptions by defining a new class that inherits from the built-in Exception class (or one of its subclasses). This allows you to raise meaningful errors specific to your application's needs. Basic Custom Exception Custom exceptions make your code easy to understand and handle errors better by clearly showing different types of errors. They help you to find and fix issues more quickly, especially in bigger and more complex programs. Example: Basic custom exception declaration In the following example, we define a simple custom exception class named "MyCustomErro" by subclassing Exception - class ... Read More
In Python 3, exception chaining allows one exception to be raised while preserving the context of the original exception. This provides a complete track, which makes it easy to understand how an error occurred during debugging. Python supports exception chaining explicitly using the raise ... from ... statement, or implicitly when a new exception is raised while handling another. Using raise ... from ... Statement This is the standard way to explicitly chain exceptions. The second exception is raised with a reference to the original exception using the from keyword, which helps to track the cause of the error. Example: ... Read More
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
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
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
In Python, sometimes an except block itself may raise an exception. Handling such exceptions properly is important to make sure that your program does not crash unexpectedly and to maintain clean error handling. Exceptions raised inside an except block can be handled by nesting try-except blocks within it. Exceptions Inside except Blocks An except block is meant to handle errors, but it can also raise exceptions if the code inside it causes errors. You need to handle these secondary exceptions to avoid program termination. Example In this example, the except block tries to divide by zero, which raises a new ... Read More
In Python, you can capture and print exception messages using try and except blocks in multiple ways, such as - Using the as keyword Using the type() function Using the traceback module Exception messages provide details about what went wrong, which is helpful for debugging and error handling. Using the 'as' Keyword You can assign the exception to a variable using the as keyword inside the except block. This allows you to access and print the actual error message. Example: Capturing ZeroDivisionError message In this example, ... Read More
The best way to log Python exceptions is by using the built-in logging module. It helps you track errors and debug your programs by capturing detailed error information. This module allows you to control where the logs are saved and organize them by their importance and source. Using logging.exception() function inside except blocks is an easy way to log errors along with the full traceback. Why Use the logging Module for Exceptions? The logging module allows you to save error messages with details like when they happened and how serious they are. It gives you more control and useful ... Read More
Virtual Functions in Derived ClassesA virtual function is declared using the virtual keyword in the base class and becomes a member function of the base class overridden by the derived class. It becomes virtual in every class which is derived from the base class. So, the keyword virtual is not necessary in the derived class while declaring redefined versions of the virtual base class function. When we use a pointer or reference to the base class to refer to a derived class object, we can call its virtual function. Syntax Following is the syntax of the virtual function in ... Read More
The string library provides several functions to manipulate and match strings. In this article, we will see how the various functions of string library functions can be used to match strings in C++. String Matching in C++ String matching is a process of locating one string in another string. Meaning, here we will find the position of a string inside another string. The string matching functions will return the position of the first occurrence of the substring in the main string. If the substring is not found, it will return a special value such as -1 or string::npos. ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP