77

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.

Thanks in advance!

kaspnord
  • 1,353
  • 2
  • 16
  • 28

5 Answers5

107
function _modulename_getData($field, $table) {
  try {
    if (empty($field)) {
      throw new Exception("The field is undefined."); 
    }
    // rest of code here...
  }
  catch (Exception $e) {
    /*
        Here you can either echo the exception message like: 
        echo $e->getMessage(); 

        Or you can throw the Exception Object $e like:
        throw $e;
    */
  }
}
AlienWebguy
  • 73,720
  • 16
  • 109
  • 137
70

To rethrow do

 throw $e;

not the message.

Chen Kinnrot
  • 19,385
  • 15
  • 74
  • 132
16

Just remove the throw from the catch block — change it to an echo or otherwise handle the error.

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.

TRiG
  • 9,249
  • 6
  • 50
  • 101
DaveRandom
  • 84,004
  • 11
  • 142
  • 168
7
throw $e->getMessage();

You try to throw a string

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

KingCrunch
  • 119,075
  • 18
  • 142
  • 167
  • What would be a better way to handle validations? Would IF statements be more appropriate in handling invalid user data? – kaspnord Jan 27 '12 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 Jan 28 '12 at 00: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 `Exception`s for control flow (here: validation) :) – KingCrunch Jul 10 '13 at 21:17
  • You can only [`throw`](http://php.net/manual/en/internals2.opcodes.throw.php) objects, so `throw $e;` or `throw new \Exception( $e->getMessage() );`. – Ismail Jan 29 '18 at 09:00
0

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

throw $e
Goms
  • 1,921
  • 3
  • 13
  • 32