我認為,如果說一個人沒有運行命令的正確權限,或者有一個冷卻時間之類的東西,而不是僅僅每隔...一次就處理一次,則拋出異常會更容易。因此,我創(chuàng)建了自己的異常,但問題是CommandService.ExecuteAsync()。Error僅返回CommandError.Exception,無法(據我所知)無法發(fā)現拋出了哪種類型的異常。我的代碼如下:try { var result = await _service.ExecuteAsync(context, argPos); if (!result.IsSuccess) switch (result.Error) { case CommandError.BadArgCount: await context.Channel.SendMessageAsync("Bad argument count."); break; case CommandError.UnknownCommand: break; case CommandError.Exception: // This is what happens instead of the catch block. break; default: await context.Channel.SendMessageAsync($"You ?? Broke ?? It ({result.ErrorReason})"); break; } } catch (Exceptions.GameCommandNotReadyException e) { // The code never gets here because of the CommandError.Exception }
1 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
您可能不想在此處使用try / catch,因為您不必要地拋出了自己的異常,然后在之后直接捕獲它。您可以將錯誤處理放入case CommandError.Exception。
如果您想更多地了解導致錯誤的異常,請執(zhí)行以下操作:
由于您正在調用該ExecuteAsync函數,因此“結果”可能不僅是類型IResult,而且是ExecuteResult類型。這意味著將存在一個屬性“ Exception”,該屬性存儲“出了什么問題”。
case CommandError.Exception:
if (result is ExecuteResult execResult)
{
//you can now access execResult.Exception to see what happened
}
break;
- 1 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消