1

How I can catch exception in zend framework 2 using the PHP base Exception?

If line is uncommented then Exception class not found and exception is uncatched.

If line is commented the namespace is null and PHP base Exception class is founded.

I can't uncommented this line because is required by zend in many places, i.g. ActionController.

How do it?
Have I to use only Zend Exceptions? which I have to use and what is the more generic zend Exception class?

 <?php namespace SecureDraw; ?> // <<----- If remove this line catch work ok!! <?php echo $this->doctype(); ?> <?php use Zend\Db\Adapter\Adapter as DbAdapter; try{ $dbAdapter = new DbAdapter(array( 'driver' => 'Pdo_Mysql', 'database' => 'securedraw', 'username' => 'root', 'password' => '', )); $sql = "select * from tablenotexist"; $statement = $dbAdapter->createStatement($sql); $sqlResult = $statement->execute(); } catch(Exception $e){ echo "hello"; } ?> 

1 Answer 1

10

You need to either add:

use Exception; 

or use:

catch (\Exception $e) { 

All the built in PHP classes exist within the root (\) namespace. The try-catch in your example is trying to match SecureDraw\Exception.

This is the same issue as How to catch exceptions in your ZF2 controllers?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.