-
Notifications
You must be signed in to change notification settings - Fork 0
Exception Handling
In python-telegram-bot
, all Telegram-related errors are encapsulated in the TelegramError
exception class and its subclasses, located in telegram.error
module.
Any error, including TelegramError
, that is raised in one of your handlers (or while calling get_updates
in the Updater
), is forwarded to all registered error handlers, so you can react to them. You can register an error handler by calling Dispatcher.add_error_handler(callback)
, where callback
is a function that takes the update
and context
. update
will be the update that caused the error (or None
if the error wasn't caused by an update, e.g. for Jobs) and context.error
the error that was raised.
Example: You're trying to send a message, but the user blocked the bot. An Unauthorized
exception, a subclass of TelegramError
, will be raised and delivered to your error handler, so you can delete it from your conversation list, if you keep one.
Note: The error handler might be only your last resort - of course you can also handle Exceptions as they occur. Only uncaught exceptions are forwarded to the error handler.
For an example on how an error handler might look like, please head over to the examples directory.
- Wiki of
python-telegram-bot
© Copyright 2015-2025 – Licensed by Creative Commons
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests