
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10400 Articles for Python

1K+ Views
While executing the statements that perform arithmetic operations, if any operation results in an illegal value, an arithmetic exception occurs (run time).In Python, ArithmeticError represents this exception, and it is the base class for all errors that occur during arithmetic operations, such as division by zero, overflow, or floating-point errors. Catching this exception helps to manage errors that arise from calculations. Catching ArithmeticError with try-except Block You can use a try-except block in Python to catch ArithmeticError and handle errors related to arithmetic operations like division by zero or overflow. This allows your program to continue running smoothly even if ... Read More

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

1K+ Views
When something goes wrong in Python, it shows a message called exception text that explains the error. You can save and use this text in your program to help with logging or fixing the problem later. You can get Python exception text using str() function, traceback module, logging, or the args attribute. This allows you to log, debug, or display user-friendly error messages in your programs. Using str() Function The easiest way to get the exception text is by converting the exception object to a string using the str() function. This returns the error message associated with the exception. Example ... Read More

237 Views
In Python, exceptions are not just error messages; they are actual objects. Understanding that exceptions are objects helps you work with them more effectively, such as accessing their attributes or creating custom exceptions. What do We mean by Exception is an Object? When an exception occurs, Python creates an instance of an exception class. This instance contains information about the error, like its type, message, and traceback. Since exceptions are objects, you can interact with them just like any other Python object. Example: Catching an exception object In the following example, we catch a ZeroDivisionError and assign it to a ... Read More

207 Views
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

2K+ Views
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

174 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

198 Views
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

401 Views
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