Table of Contents

Class CommandException

Namespace
Discord.Commands
Assembly
Discord.Net.Commands.dll

The exception that is thrown if another exception occurs during a command execution.

public class CommandException : Exception, ISerializable
Inheritance
CommandException
Implements
Inherited Members

Examples

You may use this information to handle runtime exceptions after execution. Below is an example of how you may use this:

public Task LogHandlerAsync(LogMessage logMessage)
{
    // Note that this casting method requires C#7 and up.
    if (logMessage?.Exception is CommandException cmdEx)
    {
        Console.WriteLine($"{cmdEx.GetBaseException().GetType()} was thrown while executing {cmdEx.Command.Aliases.First()} in {cmdEx.Context.Channel} by {cmdEx.Context.User}.");
    }
    return Task.CompletedTask;
}

Remarks

This Exception class is typically used when diagnosing an error thrown during the execution of a command. You will find the thrown exception passed into LogMessage.Exception, which is sent to your CommandService.Log event handler.

Constructors

CommandException(CommandInfo, ICommandContext, Exception)

Initializes a new instance of the CommandException class using a command information, a command context, and the exception that interrupted the execution.

public CommandException(CommandInfo command, ICommandContext context, Exception ex)

Parameters

command CommandInfo

The command information.

context ICommandContext

The context of the command.

ex Exception

The exception that interrupted the command execution.

Properties

Command

Gets the command that caused the exception.

public CommandInfo Command { get; }

Property Value

CommandInfo

Context

Gets the command context of the exception.

public ICommandContext Context { get; }

Property Value

ICommandContext