Ethereum: Care Error in a python script

When writing a script that interacts with the ethereum blockchain, using the openseppelin contract library or other third -party API, it is not uncommon for errors. One Common Problem is the Exception Processing of the API Calls. In this article, we will explore why you are facing an error report on classes that have not inherited baseException and provide a solution to deal with such errors.

Error Message

Ethereum: Python 3 handling error TypeError: catching classes that do not inherit from BaseException is not allowed

Error Message “TypeError: catching class inherited from basexception, is not allowed” indicates that the code is trying to get an exception in the classroom, but the exception caught is not inherited from the “basexception”, which is the base class that has a base class for all exceptions to python. This can happen if trial blocks with objects or customized classes are used.

Code

Let’s analyze the Example of the Code Fragment:

`Python

I = 0

While i <5:

I = i + 1

Try:

Sellsta = Client.get_order (Symbol = Symb, Orderid = Sellodrnum, Recvwindow = Delay)

Except Client.get_order as E:

Print ("Error:", e)

Question

In this code, “I” is a whole number variable, initialized to 0. When you use a loop with an initial value of less than 5, it repeats 4 times and then leaves the loop.

However, when you try to call client.get_order, you transferred symbol = simb, order = sellordernum, recvwindow = delay as argument. Since these values ​​are not defined in this area (i.e., they do not exist before this code block), python makes a mistake caught by the trial and exceptional block.

Solution

To prevent this problem, you need to make sure that all variables and features in the block where the exception is to have a valid initialization value. One way to do this is to use the keyword “Global” to access topical variables:

`Python

I = 0

While i <5:

Try:

Sellsta = Client.get_order (Symbol = Symb, Orderid = Sellodrnum, Recvwindow = Delay)

Except for exception as E:

Print ("Error:", st (e))

Alternatively, you can define the “symbol” and “delay” variables before the loop:

`Python

I = 0

Symbol = 'Symb'

Delay = 5

While i

Try:

Sellsta = client.get_order (symbol = symbol, order = sellodrnum, recvwindow = delay)

Except for exception as E:

Print ("Error:", st (e))

Best Practice

To Avoid Similar Issues in the Future:

  • Always define the variables with a valid initialization value in the scope where they are used.

  • Use `Global ‘when accessing local variables to make sure their values ​​are properly updated.

When applying these solutions, you should be able to successfully handle exceptions and print bug messages without contacting the typeError: catching class inherited from basexception.

staking tether usdt rate

(Visited 2 times, 2 visits today)