简体   繁体   English

Python try except 块在我不希望出现问题时运行异常代码,并且在我希望它运行异常代码时只给出错误

[英]Python try except block runs the except code when I don't expect a problem and just gives an error when I expect it to run the except code

I'm working on a discord bot in python that will wish people a happy birthday.我正在开发 python 中的 discord 机器人,祝人们生日快乐。 I plan on having users give their birthdays to the bot via a command so that it can store it in a txt file.我计划让用户通过命令将他们的生日给机器人,以便它可以将其存储在 txt 文件中。 This is the code so far:这是到目前为止的代码:

@bot.command(pass_context=True)
async def birthday(ctx,arg1,arg2,arg3,arg4):
    try:
        if (ctx.user == bot.user):
            return
        print(arg1 + ', ' + str(arg2) + ', ' + str(arg3) + ', ' + str(arg4))
    except:
        channel = bot.get_channel(channel_id)
        await channel.send('Oops! I didn\'t get that. Please try again using this format:\n!birthday Garfield 19 6 1978')

Basically, it should print the info it got if it is formatted correctly, and warn the user that there's a problem if it is not.基本上,如果格式正确,它应该打印它获得的信息,如果不是,则警告用户存在问题。 This is the error message I get:这是我收到的错误消息:

Ignoring exception in command birthday:
Traceback (most recent call last):
  File "/home/valerie/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "/home/valerie/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 790, in invoke
    await self.prepare(ctx)
  File "/home/valerie/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 751, in prepare
    await self._parse_arguments(ctx)
  File "/home/valerie/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 670, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "/home/valerie/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 516, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: arg4 is a required argument that is missing.

Why is it throwing an exception when everything should work fine and not even running the except code when there actually is a problem?为什么在一切都应该正常工作时抛出异常,甚至在实际出现问题时甚至不运行异常代码? Is there a way to fix this?有没有办法来解决这个问题?

Somewhere in your code, you are calling birthday function which is expecting 5 arguments.在您的代码中的某处,您正在调用生日 function,它期望 5 arguments。 You are not providing the required number of arguments.您没有提供所需数量的 arguments。 That's why the code is failing.这就是代码失败的原因。

If you read through the last line of the exception, you will see the error clearly called out.如果您通读异常的最后一行,您会看到清楚地指出了错误。

discord.ext.commands.errors.MissingRequiredArgument: arg4 is a required argument that is missing.

You need to provide arg4 which you are not providing.您需要提供您未提供的arg4

This is a general comment, but a little too big to fit in the comments... (so please ignore as you see fit)这是一般性评论,但有点太大了,无法放入评论中......(所以请在您认为合适的情况下忽略)

You can group you args, and use ", ".join() to make your code a bit cleaner:您可以对 args 进行分组,并使用", ".join()使您的代码更简洁:

@bot.command(pass_context=True)
async def birthday(ctx, *args):
    try:
        if (ctx.user == bot.user):
            return
        print(", ".join(args))
    except:
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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