0

Throwing simple exceptions are a common practice, so I would think it would be 'built-into' Symfony2, 'right out of the box". However, executing the following code, I get the error partly shown in this question's title and fully shown below.

   if( ( $result === FALSE ) || ( $result !== 1 ) ) {

     throw new Exception( 'update user account data function -- ' .
                          'update return value: ' . $result . ' - ' .
                          'unable to update your account!' );

   } // if( ( $result === FALSE ) || ( $result !== 1 ) ) ...

This error tells me that the new Exception class isn't supported by Symfony2 unless I also add it to my Controller's use area, but I don't know the value to add there to use this feature of PHP.

Would someone please tell me what to put in my use area?

In my code, based on the examples shown in the w3School's PHP Exception page at https://www.w3schools.com/php/php_exception.asp, the $result variable is set by the Doctrine DBAL update method (http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#update), which returns the number of rows updated or FALSE if there was an error.

I know that the update method in my code works because most of the time $result is assigned a non-zero return value and no exceptions occur, but sometimes $result is assigned a 0, still no exception, indicating that a row that is known to exist isn't actually being updated.

Is there some way to get why the update failed?

In this situation, I try to throw an exception using the above code, but then Symfony2 displays the following:

Attempted to load class "Exception" from namespace "AppBundle\Controller". Did you forget a "use" statement for e.g. "Symfony\Component\Security\Acl\Exception\Exception", "Symfony\Component\Serializer\Exception\Exception" or "Symfony\Component\Config\Definition\Exception\Exception"? 500 Internal Server Error - ClassNotFoundException

  1. in src/AppBundle/Controller/BreakinOutAdminController.php at line 1361


1358. 1359. if( ( $result === FALSE ) || ( $result !== self::ONE_ROW ) ) { 1360. 1361. throw new Exception( 'update user account data function -- ' . 1362. 'update return value: ' . $result . ' - ' . 1363. 'unable to update your account!' ); 1364.

Line 1361 is highlighted in the page Symphony2 didplays.

So, two question:

What should I put in my use area so I can throw an exception with my own message?

Is there some way to get why the update failed?

Thank you.

  • Consider using an IDE. It will save you a great deal of trouble while learning the basics of php namespaces. In any event: throw new \Exception('Oh No'); PS The error message has nothing to do with Symfony. – Cerad Dec 02 '17 at 21:30
  • THANKS! Adding the slash fixed the Exption('custom text' ) problem. –  Dec 02 '17 at 21:40
  • However, I'm still getting the condition that causes my code to throw the now working exception, **$result = 0**. I have verified that the row does exist and there shouldn't be any locks on it. Is there a way to know why the **update** method returned 0? –  Dec 02 '17 at 21:45
  • Best to stick with one question at a time. Consider creating a new question and show the query. By the way, it's very difficult understand how a count result could be false. By definition it will be a number. – Cerad Dec 02 '17 at 22:01
  • I'll do that, thanks. –  Dec 02 '17 at 22:09
  • Cant believe they downloaded this question. It had work on it, even if its wrong or you don't like it, or even if you can't do anything with this. It is useful to others. – Mbotet Jun 20 '19 at 09:24

1 Answers1

3

Attempted to load class "Exception" from (the current) namespace is a problem I make myself quite often. It's not a Symfony problem - just about default namespaces.

...new Exception (or the SplExceptions, or anything else - DateTime is another frequent one to see the same issue) just means get the class from the current namespace, unless it's specifically referenced from the use list - But Exception, or DateTime, or others are in the root namespace - \Exception. You can use them per file to bring the alias in to be used locally, or prepend the \backslash. Good unit tests that run all the code and so break when it comes to that - or static code analysis, such as phpstan, can help a lot to avoid production errors where such classes don't exist in the namespaces that are currently available to the script.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • Thanks for the detailed answer. I was wondering why the things you mentioned failed, as well. My comment about Symfony2 not automatically supporting Exception class was obviously because I didn't understanding the need to use \Exception. You seem to indicate that there is a class name that I add tothe controller's use area or even in a global use configuration file. What is the class name and what is the location and file name of the global use configuration file, if there is one? Thanks! By the way, why was the question down voted? I'm sure that others might fine the two answers helpful. –  Dec 02 '17 at 23:23
  • **it's not a Symfony issue** and there is no magical Symfony fix - it's a PHP namespace issue. Either `use Exception;`at the top of the file (or DateTime, or whatever), or prepend the use of top-level classes with the root namespace - `\`. – Alister Bulman Dec 03 '17 at 11:02