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?
- (RuntimeException $e) administrator/components/com_redirect/models/link.php line 40
- (JDatabaseExceptionExecuting $e) plugins/system/actionlogs/actionlogs.php line 192
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
- Exception class defining an unsupported database object
- Exception class defining an error executing a statement
- Exception class defining an error connecting to the database platform