
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 33676 Articles for Programming

289 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

224 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

192 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

219 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

456 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

275 Views
While executing the program, if any statement results in an abnormal value (based on the inputs) where the interpreter doesn't know how to behave. Python throws an exception.In Python, we can also throw or raise an exception manually using the raise keyword. This is helpful when you want to stop the program at a certain point and show that an error has occurred. Using raise to throw a built-in exception You can raise built-in exceptions like ValueError, TypeError, or RuntimeError using the raise statement followed by the exception name and an optional message to explain what went wrong. Example: ... Read More

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

1K+ Views
In Python, the try, except, and else statements are used to handle exceptions and define specific blocks of code that should execute based on whether an error occurs or not. This helps you manage errors and separate successful code from error-handling logic. Using "try" and "except" The try block contains code that might raise an exception. If an exception occurs, the control jumps to the except block, which contains code to handle that exception. Example: Handling division by zero In the following example, we are dividing a number by zero, which raises an exception that is handled by the except ... Read More

2K+ Views
In Python, the except clause is used to handle exceptions that may occur inside a try block. But what happens if no exceptions are raised? The except block is simply skipped. When No Exceptions Occur If the code inside the try block executes without raising any exceptions, the except block is ignored, and the program continues normally. Example: No exceptions raised In this example, we are performing a simple division that doesn't raise an exception, so the except block does not run - try: result = 10 / 2 print("Division successful:", result) except ZeroDivisionError: ... Read More