Through out the current version of Joomla (3.9.6) I find a mixture of ways that SQL exceptions are handled and I was wondering if anyone could give me some definitive situations where one should be used as opposed to the other?
If developing for the latest versions of Joomla should I just always be using JDatabaseExceptionExecuting for my query try catch blocks?
Ref:
- (RuntimeException $e) administrator/components/com_redirect/models/link.php line 40
- (JDatabaseExceptionExecuting $e) plugins/system/actionlogs/actionlogs.php line 192
Extra Info
For anyone else wanting a bit more info on this subject.
Github link: https://github.com/joomla/joomla-cms/pull/10337
Add and use new database exceptions #10337
The database API throws a RuntimeException for all error conditions. While it's good that exceptions are thrown, since this class is high in the Exception class chain (it's a direct descendent of Exception and is heavily subclassed by both PHP core and third party libraries) it's a rather generic class and doesn't offer much context about why the error was thrown without parsing the string.
This PR creates several contextual Exception classes for the database API and changes it to throw these subclasses instead of the more generic RuntimeException. As all of these new Exception classes all extend RuntimeException there is no B/C break with existing try/catch uses.
There are three JDatabse exception classs that extend RuntimeException
JDatabaseExceptionUnsupported
- Exception class defining an unsupported database object
- libraries/joomla/database/exception/unsupported.php
JDatabaseExceptionExecuting
- Exception class defining an error executing a statement
- libraries/joomla/database/exception/executing.php
JDatabaseExceptionConnecting
- Exception class defining an error connecting to the database platform
- libraries/joomla/database/exception/connecting.php