4

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

1 Answer 1

4

The difference between a RuntimeException and a JDatabaseExceptionExecuting is a matter of semantics and inheritance.

In PHP, RuntimeException extends the Exception class (the base class -- which before php7, did not provide a Throwable interface).

https://www.php.net/manual/en/class.exception.php

https://php.net/manual/en/class.runtimeexception.php

https://api.joomla.org/cms-3/classes/JDatabaseExceptionExecuting.html

Extending a class is done to provide specialised handle/details in a way that the original class does not provide.

JDatabaseExceptionExecuting is simply a more articulate/dedicated manner of catching an expected kind of exception. When you know what kind of exception is likely to be generated in a particular portion of code, you can be very deliberate about the how you want to handle it and therefore use the methods within the most appropriate class. Here's what it does:

Exception class defining an error executing a statement

getQuery() is the only method (beyond the constructor) inside the JDatabaseExceptionExecuting class which extends the RuntimeException class which extends the Exception base class.

This Stackoverflow page should be a helpful read: https://stackoverflow.com/q/41608131/2943403

Ultimately, my advice is to use JDatabaseExceptionExecuting when endeavouring to catch an exception produced while executing a query statement. Otherwise, you should use RuntimeException (or another appropriate class which has been specialised to suit the situation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.