How can PHP developers extract specific information, such as table names, from exception messages to provide context-specific error handling?

When handling exceptions in PHP applications, developers can extract specific information from exception messages to provide context-specific error handling. This can be particularly useful when dealing with database-related exceptions, where knowing the table name can help in identifying and resolving the issue. By parsing the exception message for relevant details such as table names, developers can tailor their error handling logic accordingly.

try {
    // code that may throw an exception
} catch (Exception $e) {
    $message = $e->getMessage();
    
    // Extract table name from exception message
    if (preg_match('/Table \'([^\']+)\'/', $message, $matches)) {
        $tableName = $matches[1];
        // Handle the exception based on the extracted table name
        // For example, log the error with the specific table name
        error_log("Error occurred with table: $tableName");
    } else {
        // Handle the exception in a generic way
        error_log("Generic error occurred: $message");
    }
}