# Exception handling

Torque converts all Exceptions thrown by the Database into instances of `org.apache.torque.TorqueException` or subclasses thereof. TorqueException is a checked exception.

Subclasses of TorqueException whcih are generated by database exceptions are:

- `org.apache.torque.ConstraintViolationException` - thrown whenever a write operation violates a unique key, a not-null constraint or a foreign key constraint in the database.
- `org.apache.torque.DeadlockEception` - thrown whenever the database detects a deadlock between two or more transaction which are executed simultaneously (Note: hsqldb does not seem to be able to detect deadlocks as of version 2.2.8).

All other database exceptions are mapped to TorqueException directly.

Torque also can throw TorqueExceptions or subclasses thereof without a database error, e.g. if a database is accessed via Torque when Torque is not initialized. Subclasses of TorqueExceptions thrown in special situations are:

- `org.apache.torque.NoRowsException` - thrown whenever a read operation expects to hit exactly one record but does not hit one (e.g. the generated retrieveByPk methods in the generated peer classes).
- `org.apache.torque.TooManyRowsException` - thrown whenever a read operation expects to hit at most one record but does hit more than one (e.g. by the doSelectSingleRecord methods in the peer classes).