关於报错这档事

报错

  • 也就是所谓的error,我们有多种方式做处理

第一种

  • 使用广泛类型
# cogs/event.py
# 略
@commands.Cog.listener()
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        await ctx.send(commands.CommandNotFound)
    if isinstance(error, commands.UserInputError):
        await ctx.send(commands.UserInputError)
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send(commands.MissingRequiredArgument)
    # .....
  • 可以写在前排,看起来方便,添加起来也方便

第二种

  • 使用单个command,比如.......
@commands.command()
async def slots(self, ctx, amount, mang):
    await open_account(ctx.author)
    #if amount == None:
    #    await ctx.send("请输入数字")
    #    return
  • 我们将这些拿掉後,在这底下增加
@slots.error
async def slots_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send(commands.MissingRequiredArgument)
  • 这样一来,就会有东西出来了,来看看示意图

第三种

  • 使用 try......except......
@commands.command()
async def slots(self, ctx, amount, mang):
    try:
        await ctx.send("pass")
    except Exception as e :
        await ctx.send(e)
  • 典型的侦错

预防性措施

  • 像我们先前有使用
async def withdraw(ctx, amount = None):
  • 这代表amount在默认的情况下是None,所以不会遇到错误,毕竟自己都在下方写了return

总结

  • 看个人的需求做error的处置
  • 也可以将值预设,出问题就return回去

<<:  铁人赛 Day23 -- JavaScript 初体验(一) -- Hellow World

>>:  【Day8】情蒐阶段的小工具 ─ 扫描篇(二)

工程、生命周期阶段和过程(Engineering, Life Cycle Stages, and Processes)

-NIST SP 800-160 V1 和 ISO 15288 工程(Engineering) ....

Day 11 Analyze images with the Computer Vision service

Computer vision - Process images is key to creatin...

Day 09 - Kbars 转换及储存至资料库

因前篇谈到透过api.kbars抓取1分K的资料内容,但我们在看盘或盘後分析时,可能会用到其它类型的...

铁人赛 Day2 -- SQL到底是什麽东西?讲中文好不好

SQL到底是什麽东西? 全名叫做"结构化查询语言(Structured Query Lan...

资安学习路上-Linux基础与Web基础2

网站+网页绪论 浏览器介绍(推Firefox跟Edge) 上图取自台科大资安社课教材 浏览网页发生的...