Throwing exceptions in a PHP Try Catch block

Throwing exceptions in a PHP Try Catch block

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Throwing Exceptions in a PHP Try Catch Block

You might be porting logic from a strongly typed language like C# to PHP, expecting the exception handling mechanisms to work identically. In C#, throwing an object within a try block is standard practice. However, when you attempt this in PHP, specifically within a Drupal 6 .module file, you may encounter the error message stating that objects can only be thrown within a catch block. This discrepancy stems from fundamental differences in how these languages handle exception creation and propagation.

The specific error usually arises because the code attempts to throw an instance of Exception directly inside the try block without first ensuring the object is properly instantiated or, more commonly in this context, because the developer misunderstands the nature of the string being passed versus the object itself. In your provided snippet:

if (empty($field)) {
  throw new Exception("The field is undefined."); 
}
// rest of code here...
catch (Exception $e) {
  throw $e->getMessage();
}

There are two distinct issues preventing this from working as expected. First, the throw statement requires an object instance, which new Exception(...) creates correctly. However, the second issue lies in your catch block logic. In PHP, you cannot throw a string directly (e.g., throw "message";). You must always throw an exception object. When you execute throw $e->getMessage(), you are attempting to throw the string content of the message rather than the exception object itself. This triggers a fatal error because the runtime expects an exception instance on the left side of the throw keyword, not a scalar value.

To resolve this, you must re-throw the caught exception object using the throw keyword without accessing its message property. Additionally, if you are running this code in Drupal 6, remember that this version lacks native support for PHP's built-in Exception class in many contexts unless specific compatibility layers or patches (like the standard PHP distribution included with certain distributions) are used. In a strict Drupal 6 environment, developers often relied on throwing E_USER_ERROR or utilizing custom classes that extended base exceptions if available.

If you need to validate inputs before expensive operations like database queries, catching and re-throwing is valid, provided the syntax is correct. The corrected logic for your function should look like this:

function _modulename_getData($field, $table) {
  try {
    if (empty($field)) {
      throw new Exception("The field is undefined."); 
    }
    
    // Database query logic here
    // $result = db_query("SELECT * FROM {$table} WHERE ...");
  } catch (Exception $e) {
    // Re-throw the exception object, not the message string
    throw $e; 
  }
  
  return null; // Or handle success case
}

This pattern allows you to maintain a clean separation between validation logic and business logic. The try block handles the potential failure of the validation step. If an Exception is thrown, it is caught, and then immediately re-thrown. This preserves the original stack trace and exception type, which is crucial for debugging later in the application lifecycle.

The reason you cannot simply pass the message string back to the caller without wrapping it in an exception object is that PHP's error handling architecture distinguishes between fatal errors (which halt execution) and exceptions (which can be caught). By throwing a string, you break this contract. If you intend to log the error or pass it up the call stack, the object must remain intact.

For modern projects migrating from Drupal 6 or those building on similar frameworks like Laravel, the approach remains consistent: validate early, throw exceptions for invalid states, and ensure catch blocks re-throw objects rather than scalars. If you are working with a framework that provides its own exception hierarchy, such as InvalidArgumentException or LogicException, consider using those instead of the generic Exception class to provide more semantic context about why the validation failed.

Our network