82

I have a PHP function in a Drupal 6 .module file. I am attempting to run initial variable validations prior to executing more intensive tasks (such as database queries). In C#, I used to implement IF statements at the beginning of my Try block that threw new exceptions if a validation failed. The thrown exception would be caught in the Catch block. The following is my PHP code:

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

However, when I try to run the code, it's telling me that objects can only be thrown within the Catch block.

2
  • 3
    tl;dr "throw $e->getMessage();" should be "throw $e;" Commented Jul 4, 2016 at 18:40
  • Same code seems to produce: Parse error: syntax error, unexpected 'throw' (T_THROW). I guess no longer supported.
    – HippoDuck
    Commented Sep 23, 2020 at 13:40

5 Answers 5

120

Please note that this particular code example makes no sense, as you can simply remove the try-catch and have the same result:

function _modulename_getData($field, $table) {
    if (empty($field)) {
      throw new Exception("The field is undefined."); 
    }
    // rest of code here...
}

so Exception will be thrown and can be caught or handled elsewhere.

In case you are going to write some handling code, that may result in re-throwing the exception, you have to throw the Exception object, not a string returned by the getMessage() method:

function _modulename_getData($field, $table) {
  try {
    if (empty($field)) {
      throw new Exception("The field is undefined."); 
    }
    // rest of code here...
  }
  catch (Exception $e) {
    if (some condition) {
        // do some handling
    } else {
        // the error is irrecoverable
        throw $e;
    }
  }
}

or if you have to do some recovery, as it is often used with database transactions: do the recovery and then re-throw:

  catch (Exception $e) {
      $db->rollback(); // rollback the transaction
      throw $e; // let the error to be handled the usual way
    }
  }
1
  • 1
    Yes, both of your suggestions within the Catch block will resolve my problem. Thanks!
    – kaspnord
    Commented Jan 27, 2012 at 23:15
75

To rethrow do

 throw $e;

not the message.

2
  • 6
    Would that retain the stack information though, or would it overwrite it?
    – DanMan
    Commented Dec 6, 2013 at 15:48
  • 4
    retain, to override throw a new e Commented Jan 21, 2015 at 15:26
16

Just remove the throw if you are going to handle the error, or remove the try-catch block altogether, if all you need is the error message.

It's not telling you that objects can only be thrown in the catch block, it's telling you that only objects can be thrown, and the location of the error is in the catch block — there is a difference.

In the catch block you are trying to throw something you just caught — which in this context makes little sense anyway — and the thing you are trying to throw is a string.

A real-world analogy of what you are doing is catching a ball, then trying to throw just the manufacturer's logo somewhere else. You can only throw a whole object, not a property of the object.

0
8

You tried to throw a string:

throw $e->getMessage();

You can only throw objects that implement \Throwable, e.g. \Exception.

As a sidenote: Exceptions are usually to define exceptional states of the application and not for error messages after validation. Its not an exception, when a user gives you invalid data

4
  • What would be a better way to handle validations? Would IF statements be more appropriate in handling invalid user data?
    – kaspnord
    Commented Jan 27, 2012 at 23:08
  • In short: Yes. In long: Treat the occurence of invalid arguments like any other situation that can occur in your app and show an other useful (error-)page. function validateField($validate) {return empty($validate);} and somewhere for example if validateField($x){ echo "Field is empty";}else{doSomethingUseful();}
    – KingCrunch
    Commented Jan 28, 2012 at 0:15
  • 4
    @lazycommit "invalid data" is a little bit to general. If you mean "invalid values given from a user", than they are not invalid from the applications point of view, because the application must expect this and therefore must treat them appropriate (--> validation). If you pass invalid data later during processing (from a backend, or because you didn't validate properly), then yes: Exception. To sum it up: Do not use Exceptions for control flow (here: validation) :)
    – KingCrunch
    Commented Jul 10, 2013 at 21:17
  • 1
    You can only throw objects, so throw $e; or throw new \Exception( $e->getMessage() );.
    – Ismail
    Commented Jan 29, 2018 at 9:00
0

Throw needs an object instantiated by \Exception. Just the $e catched can play the trick.

throw $e

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.