【问题标题】:Try Catch in Discord.js does not catch在 Discord.js 中尝试 Catch 无法捕获
【发布时间】:2021-01-11 01:53:34
【问题描述】:

我有一个命令处理程序,如果在加载命令时发生错误,它应该捕获错误,但它不会捕获错误。我需要这个,因为我的机器人会在出现错误时崩溃,而我不希望那样。

这是模块的代码:

for(const file of utilsCommandFiles) {
    try {
    const command = require(`./commands/utils/${file}`)
    console.log(`[INFORMATION] HYPX: Utils-Modul "${command.name}" wurde erfolgreich geladen.`)
    utilsCMDs.set(command.name, command)
    } catch(e) {
        const command = require(`./commands/utils/${file}`)
        console.error(`[ERROR] HYPX: Modul ${command.name} konnte nicht geladen werden.`)
        console.log(e)

    }
}

【问题讨论】:

  • “不抓”是什么意思?
  • 异常没有被捕获
  • 所以它通过了,我得到了堆栈跟踪,机器人崩溃了
  • 请发布堆栈跟踪
  • 堆栈跟踪是我所做的错误。(我在需要加载的类中写了一些便便)所以没有堆栈跟踪。它只是没有捕捉到异常

标签: javascript discord.js


【解决方案1】:

我发现了我的错误。无需回答。

for(const file of utilsCommandFiles) {
    try {
    const command = require(`./commands/utils/${file}`)
    commands.set(command.name, command)
    console.log(`[INFORMATION] HYPX: Utils-Modul "${command.name}" wurde erfolgreich geladen.`)
    } catch(e) {
        console.error(`[ERROR] HYPX: Modul konnte nicht geladen werden.`)
        console.log(e)

    }
}

我只是忘了在 TryCatch 块中const command = require(./commands/utils/${file})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2017-08-07
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多