Catch IndexError Exception in Python

Sarika singh
Updated on 22-May-2025 17:11:18

4K+ Views

In Python, an IndexError occurs when you try to access a position (index) in a list, tuple, or similar collection that isn't there (does not exist). It means your program is trying to access the elements that are not available in the sequence (object). Using try-except to Catch IndexError You can use a try-except block to catch (handle) an IndexError and stop your program from crashing if you try to access an index that doesn't exist (or invalid). Example In this example, we try to access the 5th index of a list that only has 3 elements, which causes an ... Read More

Catch IOError Exception in Python

Sarika Singh
Updated on 22-May-2025 16:57:13

8K+ Views

In Python, an IOError (or OSError in latest versions) occurs when an input/output operation fails. For example, when we are trying to read a file that doesn’t exist, writing to a file that is read-only, or accessing a corrupted device. You can catch IOError using a try-except block to handle file input/output errors in Python. For compatibility with Python 3, we need to use OSError or catch both using a tuple. Using try-except Block You can catch IOError using a try-except block. This helps you handle file-related errors without crashing the program. Example In this example, we try to open ... Read More

Check Substring in Another String in Python

Sarika Singh
Updated on 22-May-2025 16:46:11

742 Views

In Python, you can check whether a substring exists within another string using Python in operator, or string methods like find(), index(), and __contains__(). A string in Python is a sequence of characters that is enclosed in quotes. You can use either single quotes '...' or double quotes "..." to write a string, like this - "Hello" //double quotes 'Python' //single quote A substring in Python simply means a part of a string. For example, text = "Python" part = "tho" Here,  "tho" is a substring of the string "Python". Using the in operator We can check ... Read More

Why Python Exceptions are Named Error (e.g. ZeroDivisionError, NameError, TypeError)

Sarika Singh
Updated on 22-May-2025 15:31:32

197 Views

In Python, exception names usually end with "Error" (like ZeroDivisionError, NameError, and TypeError). This clearly shows that they are related to problems that happen while the program is running. Using this naming style makes error messages easier to read, helps with debugging. Why exceptions end with "Error" An exception is a kind of (run-time) error. Having the word "Error" in the name of the exception may help us realize that there is an issue when we encounter an exception. It follows a logical naming convention similar to other programming languages like Java and C++, that also use names ending in ... Read More

Pass a Variable to an Exception in Python

Sarika Singh
Updated on 22-May-2025 15:17:06

2K+ Views

To pass a variable to an exception in Python, provide the variable as an argument when raising the exception. For custom exceptions, store the variable in an attribute. You can pass variables like strings or numbers directly into built-in exceptions to include dynamic data in the error message. Example: Passing a variable to a ValueError In this example, we are passing a variable containing an invalid input message to a ValueError - value = "abc123" try: raise ValueError(f"Invalid input: {value}") except ValueError as e: print("Caught exception:", e) We get the following output - ... Read More

Pre-Increment and Post-Increment Concept in C/C++

Tapas Kumar Ghosh
Updated on 21-May-2025 18:16:42

12K+ Views

Both pre-increment and post-increment are used to represent the expression of increasing a value by adding 1. Their behavior are different because pre-increment (++i) increases the value before it is used in an expression, while post-increment (i++) increases it after. Below is the key-terms of pre-increment and post-increment in C/C++: Pre-increment (++i) : Before assigning the value to the variable, the value is incremented by one. Post-increment (i++) : After assigning the value to the variable, the value is incremented. Syntax of Using Pre and Post Increment Operators The following is the basic syntax of pre-increment and post-increment ... Read More

Reverse a String in C/C++

Tapas Kumar Ghosh
Updated on 21-May-2025 18:03:29

2K+ Views

There can be multiple ways to reverse a string in C and C++. In this article, we will learn reversing a string using different approaches with the appropriate examples. Consider the below example with input and output scenarios: Input // characters representation from left to right inp = "Tutorialspoint" Output // characters representation from right to left out = tniopslairotuT Now, we have following approaches to solve the reverse of a given approach: Using Two Pointer Using reverse() Function Using strlen() Function Reverse String ... Read More

Add Two Distances in Inch-Feet System Using Structures in C++

Tapas Kumar Ghosh
Updated on 21-May-2025 17:29:39

800 Views

A structure is a collection of items of different data types. It is very useful in creating complex data structures with different data type records. A structure is defined with the struct keyword. An example of a structure is as follows: struct DistanceFI { int feet; int inch; }; The above structure defines a distance in the form of feet and inches. Example of Adding Two Distances (inch-feet) Using Structure The program uses a structure named DistanceFI to represent a distance in terms of feet and inches. It creates two different ... Read More

Sort Elements in Lexicographical Order in C++

Tapas Kumar Ghosh
Updated on 21-May-2025 17:28:52

2K+ Views

Lexicographical order denotes the way the words are ordered in a list, based on alphabetical order according to their alphabets. For example: List of words: Harry Adam Sam Lexicographical order of words: Adam Harry Sam Sorting String Based on Lexicographical Order To implement the string in lexicographical order, use the two different iterators: one to point at the current string, and the other to compare it with the next strings in the array. If the first iterator points to a string that is greater than the one pointed to by the second iterator, then we swap ... Read More

Differences Between Default Constructor and Parameterized Constructor in Java

Shriansh Kumar
Updated on 21-May-2025 16:55:43

15K+ Views

The default and parameterized constructors are two types of Constructor in Java. The constructor is a special member of a Java class whose name is the same as the class name. It is used to assign values to a class variable at the time of object creation. In this article, we are going to discuss the difference between default and parameterized constructors. Default Constructor When we do not add a constructor to a Java class. The compiler adds a default constructor implicitly. It accepts 0 arguments. If we do not initialize the instance variables of a class, a default constructor will ... Read More

Advertisements