Explicitly construct an AsyncMysqlException

public function __construct(
  AsyncMysqlErrorResult $result,

Normally, you will catch an AsyncMysqlException, but if you want to explictly construct one and, for example, throw it for some given reason, then you pass it an AsyncMysqlErrorResult.


  • AsyncMysqlErrorResult $result - An AsyncMysqlErrorResult that contains the error information for the failed operation.


The following example shows how to catch an AsyncMysqlException. Normally you would construct one implicitly via a try/catch block, like we did in this example.

The two current subclasses of AsyncMysqlException are AsyncMysqlConnectionException for connection problems and AsyncMysqlQueryException for querying issues.

Note that you can explicitly construct one by creating an object like new AsyncMysqlException(AsyncMysqlErrorResult $result). But this is not normally done.

use \Hack\UserDocumentation\API\Examples\AsyncMysql\ConnectionInfo as CI;

async function connect(
  \AsyncMysqlConnectionPool $pool,
): Awaitable<\AsyncMysqlConnection> {
  return await $pool->connect(
async function simple_query(): Awaitable<?string> {
  $pool = new \AsyncMysqlConnectionPool(darray[]);
  $conn = null;
  try {
    $conn = await connect($pool);
    $result = await $conn->query(
      'SELECT name FROM test_table WHERE userID = 1',
    return $result->vectorRows()[0][0];
  } catch (\AsyncMysqlConnectException $ex) { // implicitly constructed
    return "Connection Exception";
  } catch (\AsyncMysqlQueryException $ex) { // implicitly constructed
    return "Query Exception";
  } catch (\AsyncMysqlException $ex) {
    return null;
  } finally {
    if ($conn) {

async function run(): Awaitable<void> {
  $r = await simple_query();
string(20) "Connection Exception"