Python Articles

Page 32 of 855

How to implement a custom Python Exception with custom message?

Manogna
Manogna
Updated on 13-Mar-2026 335 Views

In Python, you can create custom exceptions by inheriting from built-in exception classes. This allows you to define specific error types with meaningful messages for your application. Custom exceptions help make your code more readable and provide better error handling. Creating a Custom Exception Class To implement a custom Python exception with a custom message, you need to create a class that inherits from a built-in exception class like Exception, ValueError, or RuntimeError. The custom class should have an __init__ method to store the custom message. Example Here's how to create and use a custom exception ...

Read More

How can I write a try/except block that catches all Python exceptions?

Manogna
Manogna
Updated on 13-Mar-2026 259 Views

It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn't − try: # do_something() pass except: print("Exception Caught!") However, this will also catch exceptions like KeyboardInterrupt and SystemExit that we may not be interested in handling. This can make it difficult to interrupt your program or cause other unexpected behaviors. Better Approach with Exception Re-raising A better approach is to catch all exceptions but re-raise them after logging or handling. Here's a complete ...

Read More

How to catch StandardError Exception in Python?

Sarika Singh
Sarika Singh
Updated on 13-Mar-2026 612 Views

In Python 2, StandardError was a built-in exception class that served as 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. StandardError in Python 2 The StandardError class in Python 2 was designed to catch all standard exceptions that ...

Read More

Where can I find good reference document on python exceptions?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 13-Mar-2026 132 Views

Finding reliable documentation on Python exceptions is crucial for effective error handling. The following resources provide comprehensive information on Python exceptions. Official Python Documentation The Python official documentation is the most authoritative source for exception reference − Python 3.x (Latest): https://docs.python.org/3/library/exceptions.html Python 2.x (Legacy): https://docs.python.org/2/library/exceptions.html Note: Python 2 reached end-of-life in January 2020. It's recommended to use Python 3 documentation for current projects. What You'll Find in the Documentation The official documentation covers − Built-in exceptions − Complete list of all standard exception classes ...

Read More

How do you properly ignore Exceptions in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 13-Mar-2026 632 Views

Ignoring exceptions in Python can be done using try-except blocks with a pass statement. Here are the proper approaches − Method 1: Using Bare except This approach catches all exceptions, including system-level exceptions − try: x, y = 7, 0 z = x / y print(f"Result: {z}") except: pass print("Program continues...") The output of the above code is − Program continues... Method 2: Using except Exception This is ...

Read More

Python program to count the elements in a list until an element is a Tuple?

AmitDiwan
AmitDiwan
Updated on 12-Mar-2026 680 Views

In this article, we will count the elements in a list until an element is a Tuple. A Python list can contain items of different types, including tuples. The goal is to iterate through the list and count how many elements appear before the first tuple is encountered. For example, given the following list − mylist = [25, 50, 75, 100, 125, (20, 175, 100, 87), 500] There are 5 elements (25, 50, 75, 100, 125) before the first tuple (20, 175, 100, 87), so the count is 5. Using isinstance() The isinstance() function checks whether an object ...

Read More

Python program to find the number occurring odd number of times using Lambda expression and reduce function

AmitDiwan
AmitDiwan
Updated on 12-Mar-2026 6 Views

We can find the number occurring an odd number of times in a list using a Lambda expression and the reduce() function from the functools module. The approach uses the XOR (^) bitwise operator − XORing a number with itself cancels it out (returns 0), so after XORing all elements, only the number that appears an odd number of times remains. For example, given the following input − ele = [10, 20, 20, 30, 40, 20, 30, 10, 40] The output is 20, because 20 occurs three times (an odd count), while all other integers appear an even ...

Read More

Difference between attributes and properties in python?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 8K+ Views

In Python, everything is an object, and every object has attributes and methods. Attributes are data variables that describe the object (such as name, age, or height), while properties are a special kind of attribute that use getter, setter, and deleter methods to control access to the underlying data. Attributes in Python Attributes are variables that belong to an object. They are usually defined in the __init__ method and accessed using dot notation ? Example class Dog: def __init__(self, name, age): self.name = name ...

Read More

Saving Python functions in a Sqlite table

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 356 Views

In Python, you can store a function as a text string in a SQLite database using the sqlite3 module. You cannot run the function directly from the database, but you can retrieve the code later and execute it using Python's exec() or eval() functions. This is helpful when you want to store or update function logic, like in code editors, plugins, or apps that run user-created scripts. Using sqlite3 and exec() Function The sqlite3 module is used to create and work with SQLite databases in Python. The exec() function can run Python code that is stored as a string, including ...

Read More

Why Python functions are hashable?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 178 Views

An object in Python is hashable if it has a hash value that remains the same during its lifetime. It must have a __hash__() method and be comparable to other objects via __eq__(). If two hashable objects are equal when compared, they have the same hash value. Being hashable makes an object usable as a dictionary key and a set member, since these data structures use hash values internally. All immutable built-in objects in Python are hashable. Mutable containers like lists and dictionaries are not hashable, while immutable containers like tuples are. Objects that are instances of user-defined classes are ...

Read More
Showing 311–320 of 8,547 articles
« Prev 1 30 31 32 33 34 855 Next »
Advertisements