Handling Exceptions
As you’ve seen already, discord.py
is an event-driven system. This focus on events extends even to exceptions. When one event handler raises an Exception
, Discord calls on_error()
. The default behavior of on_error()
is to write the error message and stack trace to stderr
. To test this, you’ll add a special message handler to on_message()
.
00:00
This is part 10. Now, let’s take a look at exception handling. As you would have already seen, discord.py
is an event-driven system. This focus on events extends all the way to exceptions.
00:14
When one event handler raises an Exception
, Discord calls on_error()
. The default behavior of on_error()
is to write the error message and stack trace to stderr
(standard error). To test this, add a special message handler to on_message()
.
00:31
You can see it’s been added here and it will raise the DiscordException
. This new raise-exception
message handler allows you to raise a DiscordException
on command. Run the program, wait for it to connect,
00:48
and type raise-exception
into the Discord channel. You should now see the Exception
that was raised by your on_message()
handler in the console, just at the bottom here, Discord.errors.DiscordException
, so you know that the exception handler is working.
01:06
The Exception
was caught by the default error handler, so the output contains a message Ignoring exception in on_message
.
01:14
If we go back—there it is. Ignoring exception in on_message
, just there. Let’s fix that by handling
01:22
that particular error. In order to do so, you’ll catch the DiscordException
and write to a file, instead. The on_error()
event handler takes the event
as the first argument.
01:34
In this case, we expect the event
to be 'on_message'
.
01:39
It also accepts *args
and **kwargs
as flexible, positional and keyword arguments passed to the original event handler. So, since on_message()
takes a single argument, message
, we expect args[0]
to be the message
that the user sent in the Discord channel.
01:58
If the Exception
originated in the on_message()
event handler, which is what this line does, you write a formatted string to the file err.log
, which is what this line does. If another event raises an Exception
, then we simply want our handler to re-raise the Exception
to invoke the default behavior. Rerun bot.py
and send the raise-exception
message again, to view the output in err.log
.
02:35
This is err.log
, I’ve just done a little bit of formatting, so it’s a little bit more readable because it comes, by default, in one big line, makes it a little bit annoying, but you can still see it. So, as you can see, instead of only a stack trace, you have a more informative error showing the message
that caused on_message()
to raise the DiscordException
.
02:57
Save to a file for longer persistence. As a quick technical note, if you wish to take the actual Exception
into account when you are writing your error messages to the error log, or err.log
, then you can use functions from sys
, such as exc_info()
. Well done!
03:16
You now have some experience in handling different events and interacting with Discord APIs. Next up, you will learn about a subclass of Client
called Bot
, which implements some useful, bot-specific functionality.
Become a Member to join the conversation.