How to pass argument to an Exception in Python? Last Updated : 07 Oct, 2021 Comments Improve Suggest changes Like Article Like Report There might arise a situation where there is a need for additional information from an exception raised by Python. Python has two types of exceptions namely, Built-In Exceptions and User-Defined Exceptions.Why use Argument in Exceptions? Using arguments for Exceptions in Python is useful for the following reasons: It can be used to gain additional information about the error encountered. As contents of an Argument can vary depending upon different types of Exceptions in Python, Variables can be supplied to the Exceptions to capture the essence of the encountered errors. Same error can occur of different causes, Arguments helps us identify the specific cause for an error using the except clause. It can also be used to trap multiple exceptions, by using a variable to follow the tuple of Exceptions. Arguments in Built-in Exceptions: The below codes demonstrates use of Argument with Built-in Exceptions:Example 1: Python3 try: b = float(100 + 50 / 0) except Exception as Argument: print( 'This is the Argument\n', Argument) Output: This is the Argument division by zero Example 2: Python3 my_string = "GeeksForGeeks" try: b = float(my_string / 20) except Exception as Argument: print( 'This is the Argument\n', Argument) Output: This is the Argument unsupported operand type(s) for /: 'str' and 'int' Arguments in User-defined Exceptions: The below codes demonstrates use of Argument with User-defined Exceptions:Example 1: Python3 # create user-defined exception # derived from super class Exception class MyError(Exception): # Constructor or Initializer def __init__(self, value): self.value = value # __str__ is to print() the value def __str__(self): return(repr(self.value)) try: raise(MyError("Some Error Data")) # Value of Exception is stored in error except MyError as Argument: print('This is the Argument\n', Argument) Output: This is the Argument 'Some Error Data' Example 2: Python3 # class Error is derived from super class Exception class Error(Exception): # Error is derived class for Exception, but # Base class for exceptions in this module pass class TransitionError(Error): # Raised when an operation attempts a state # transition that's not allowed. def __init__(self, prev, nex, msg): self.prev = prev self.next = nex try: raise(TransitionError(2, 3 * 2, "Not Allowed")) # Value of Exception is stored in error except TransitionError as Argument: print('Exception occurred: ', Argument) Output: Exception occurred: (2, 6, 'Not Allowed') Comment More infoAdvertise with us Next Article How to pass argument to an Exception in Python? R RajuKumar19 Follow Improve Article Tags : Python Python-exceptions Practice Tags : python Similar Reads How to Ignore an Exception and Proceed in Python There are a lot of times when in order to prototype fast, we need to ignore a few exceptions to get to the final result and then fix them later. In this article, we will see how we can ignore an exception in Python and proceed. Demonstrate Exception in Python Before seeing the method to solve it, l 3 min read How to log a Python exception? To log an exception in Python we can use a logging module and through that, we can log the error. The logging module provides a set of functions for simple logging and the following purposes DEBUGINFOWARNINGERRORCRITICALLog a Python Exception Examples Example 1:Logging an exception in Python with an 2 min read How to handle a Python Exception in a List Comprehension? In Python, there are no special built-in methods to handle exceptions in the list comprehensions. However, exceptions can be managed using helper functions, try-except blocks or custom exception handling. Below are examples of how to handle common exceptions during list comprehension operations:Hand 3 min read How to catch a NumPy warning like an exception in Python An N-dimensional array that is used in linear algebra is called NumPy. Whenever the user tries to perform some action that is impossible or tries to capture an item that doesn't exist, NumPy usually throws an error, like it's an exception. Similarly, in a Numpy array, when the user tries to perform 3 min read How to Break a Function in Python? In Python, breaking a function allows us to exit from loops within the function. With the help of the return statement and the break keyword, we can control the flow of the loop.Using return keywordThis statement immediately terminates a function and optionally returns a value. Once return is execut 2 min read Like