Advertisement



< Prev
Next >



Django Exceptions





What is an Exception in Django?


Exception is an unusual exceptional event(describing an error), which occurs only during the runtime of a program.

During the execution of a program, when an exception occurs, it halts the normal execution of the program and it ends abruptly. That's why we should handle an exception when it is thrown by the program. The process of handling an exception is called Exception Handling.




Some situations due to which an Exception may arise :






What happens when an Exception is thrown?


Exceptions are thrown at the runtime of a program. If an exception, which is thrown during the runtime of a program, is not handled, the program terminates right away without executing the remaining statements. Let us understand this by an example here.



Looking at the diagram above, it is easy to understand that if an exception is thrown when statement2 is executed, the program ends abruptly without executing any statement after it. Hence, statement3 and statement4 will not be executed as a result of an exception at statement2.



Advertisement




Some important types of Exceptions -:


Django raises and supports its own exceptions classes and also supports all the core python exceptions classes. First, let us see the Django core exception classes which are defined in django.core.exceptions, as shown in the table below.

Django Core Exception Classes Description
AppRegistryNotReady
This exception is raised when attempting to use models before the app loading process.

ObjectDoesNotExist
The base class for DoesNotExist exceptions and it will catch all kinds of DoesNotExist exceptions.

EmptyResultSet
This exception may be raised during the query generation i.e.when a query doesn't result any result.

FieldDoesNotExist
This exception is raised by the model when the requested field does not exist on the model or on the model's parents.

MultipleObjectsReturned
This exception is raised by a query, when one object was expected but multiple objects were returned.

Suspicious Operation

This exception is raised when a user has performed an suspicious operation from a security point of view, such as tampering with a session cookie.

PermissionDenied

This exception is raised when a user doesn not have a permission to perform a certain action.

ViewDoesNotExist
This type of exception is raised by django.urls when a requested view does not exist.

MiddlewareNotUsed
This type of exception is raised when a middleware is not used in a server configuration.

ImproperlyConfigured
This type of exception is raised when Django is improperly configured, for example - when a value in settings.py is incorrect.

FieldError
This type of exception is raised when there is a problem with a model's field, which could happen due to one of the following reasons:

  • A field name is invalid.
  • A field cannot be determined from a keyword in the query.
  • A field in the model is clashing with the same field name in its abstract base class.


ValidationError
This type of exception is when data fails form or model field validation.






Django URL Resolver Exceptions


Let us see the Django URL resolver exception classes which are defined in django.urls, as shown in the table below.

Django URL Resolver Exception Classes Description
Resolver404
This exception is raised if the path passed to resolve() doesn’t map to a view.

NoReverseMatch
This exception is raised by django.urls when a matching URL defined in URLconf cannot be identified based on the supplied parameters.






Django Database Exceptions


Let us see the Django Database exception classes which are defined in django.db, as shown in the table below.

Django Database Classes Description
Error
This is the base class of all other error exceptions. You can use this to catch all errors with one single except statement.
Error
This is the base class of all other error exceptions. You can use this to catch all errors with one single except statement.
InterfaceError
This exception is raised for the errors that are related to the database interface rather than the database itself.
DatabaseError
This exception is raised for the errors that are related to the database.
DataError
This exception is raised for the errors which are raised while processing data like, division by zero, numeric value out of range.
OperationalError
This exception is raised for the errors that are related to the database's operation and not necessarily under the control of the programmer, for example - an unexpected disconnect occurs, a transaction could not be processed, a memory allocation error occurred during processing, etc
IntegrityError
This exception is raised when the relational integrity of the database is affected, for example - when a foreign key check has failed.
InternalError
This exception raised when the database encounters an internal error, for example - the cursor is not valid anymore.
ProgrammingError
This exception is raised for the programming errors, for example - table not found or already exists, syntax error in the SQL statements .
NotSupportedError
This exception is raised in a situation when a method or database API used, is not supported by the database..





Django Http Exceptions


Let us see the Django Http exception classes which are defined in django.http, as shown in the table below.

Django Http Exception Class Description
UnreadablePostError
This type of exception is raised when a user cancels an upload.





Django Transaction Exceptions


Let us see the Django Transaction exceptions are defined in django.db.transaction, as shown in the table below.

Django Transaction Exception Class Description
TransactionManagementError
This type of exception is raisedfor all problems related to database transactions.






Django Testing Exceptions


Let us see the Django Testing exceptions are defined in django.test, as shown in the table below.

Django Testing Exception Class Description
RedirectCycleError
This type of exception is raised when the test client detects a loop or an overly long chain of redirects.



Apart from the above described Django specific exceptions, Django also raises built-in Python exceptions.

In the next few articles, we will show you a exception situation in a Django project and will also show how to handle the exceptions by using the try, except finally and raise keywords.



Please share this article -





< Prev
Next >
< Django Serialization
Django Exception Handling >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement