Catch ImportError Exception in Python

Sarika Singh
Updated on 07-Jun-2025 19:42:48

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.  Catching the ImportError ExceptionLike any other exception, we can catch the ImportError exception using the try-except blocks. In this article, we discuss various scenarios where the ImportError exception occurs, with examples, and catch the generated exception using the try-except blocks in each scenario.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.Example: Importing a ... Read More

Catch ValueError Using Exception in Python

Sarika Singh
Updated on 07-Jun-2025 19:10:08

363 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 pass an invalid value ... Read More

Catch ZeroDivisionError Exception in Python

Sarika Singh
Updated on 07-Jun-2025 18:13:43

582 Views

ZeroDivisionError Exception in Python The ZeroDivisionError is raised in Python when a number is divided by zero. Since dividing by zero is mathematically undefined, Python throws this error. In this article, you will learn how to catch and handle ZeroDivisionError using the try-except block. Usually, when an exception occurs, the program will be terminated abruptly, leaving the statements after the exception unexecuted. By handling this exception, the execution of the program continues even after the ZeroDivisionError exception (an invalid division attempt). The ZeroDivisionError generally occurs in the following cases - Dividing any number by zero using / ... Read More

Catch StandardError Exception in Python

Sarika Singh
Updated on 07-Jun-2025 17:48:24

522 Views

StandardError in Python 2In Python 2, StandardError was a built-in exception class that is 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. Example Following is a basic example of raising the StandardError exception in Python 2 - try: value ... Read More

Catch StopIteration Exception in Python

Sarika Singh
Updated on 07-Jun-2025 17:15:16

1K+ Views

StopIteration Exception in Python  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__() 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? When can retrieve the contents of an Iterator object,  using the Python built-in function next() or the __next__() method (of the iterator object). The next() function internally calls the _next__() method. These methods return the ... Read More

Highlight Selected Tab of a JTabbedPane in Java

Alshifa Hasnain
Updated on 06-Jun-2025 19:41:24

830 Views

In this article, we will learn to highlight the selected tab of a JTabbedPane in Java. JTabbedPane is a common Swing component that is used to organize content into multiple tabs, while highlighting a selected tab will improve the GUI and make the interface interactive. What is a JTabbedPane? A JTabbedPane is a subclass of the JComponent class, and it can provide easy access to more than one panel. Each tab is associated with a single component that can be displayed when the tab is selected. A JTabbedPane can generate a ChangeListener interface when a tab is selected. What do ... Read More

Clear the cin Buffer in C++

Aman Kumar
Updated on 06-Jun-2025 19:39:15

2K+ Views

Before knowing the solution to clearing the cin buffer in C++, let us first see what the buffer is in C++. What is The Buffer in C++ A buffer is a temporary storage area that holds data while standard I/O devices are in use. In C++, a stream also work as a buffer. When a key is pressed, the input is not immediately sent to the program; instead, it is stored in a buffer. The operating system keeps this data in the buffer until the program is allocated time to process it. Why We Clear The cin Buffer in C++ ... Read More

Block Swap Algorithm for Array Rotation in JavaScript

Ravi Ranjan
Updated on 06-Jun-2025 19:37:06

322 Views

To implement block swap algorithm for array rotation, we will be understanding three different approaches. Rotation of the array elements means moving the elements of the given array to either the left or right side by some number of specific positions. Block swap algorithm for array rotation means to rotate the elements of the array by a given number but not using the rotation but the swapping technique. In this article we are having an array and a variable d by which array will be rotated, our task is to write a JavaScript program for block swap algorithm for array ... Read More

Check If Matrix Is Upper Triangular in JavaScript

Ravi Ranjan
Updated on 06-Jun-2025 19:36:38

236 Views

To check if matrix is upper triangular matrix, we check if all the elements below the main diagonal is 0. We are going to discuss two different approaches and compare their complexities. In this article we are having a 2D square matrix, our task is to write a JavaScript program to check if matrix is upper triangular matrix. Users must be familiar with upper triangular matrix, nested for loop, conditional statement and javascript methods. Example Input: Matrix: [[1, 2, 3, 4], 0, 5, 6, 7], ... Read More

Precision of Floating Point Numbers in C++

Tapas Kumar Ghosh
Updated on 06-Jun-2025 19:32:33

8K+ Views

Precision of floating point numbers is the accuracy upto which a floating point number can hold the values after decimal. For example, 10/6 = 1.6666666… these have recurring decimals which can take infinite memory spaces to be stored. To avoid memory overflow in such cases the compiler set a precision limit to the number. For float values in C++, the precision is set to 6-7 digit after that if the decimal recurs it will discard the value. So, to avoid any major losses when this discarding takes place there are methods and libraries that support the precision is float values. ... Read More

Advertisements