简体   繁体   中英

How to raise more helpful error message than “takes exactly x arguments ( y given)”

When a user calls a method and does not provide the proper number of arguments, I would like to be able to tell them which arguments they are missing.

However, my understanding is that the method that is causing the error is not actually being called, so then I can't write code within the method itself that would raise such an error.

Ultimately, I would like to be able to catch this error, and print a message that states the required arguments. This way, a user can find out which requirement they didn't meet.

The standard error message seems quite adequate, in my opinion. What more would you want to tell the user?

But to answer the question, you could have your method take a variable number of arguments (ie *args or **kwargs ) and then count the number of arguments within the method and raise your own error if necessary.

Or you could provide default (but nonsensical) values for each argument, and then check for the nonsense values within the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM