Throwing exceptions in a PHP Try Catch block

后端 未结 5 1927
情书的邮戳
情书的邮戳 2020-12-24 00:04

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

5条回答
  •  再見小時候
    2020-12-24 00:42

    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.

提交回复
热议问题