josepmra
josepmra

Reputation: 627

zend framework 2 how catching exceptions?

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";
        }
    ?>

Upvotes: 1

Views: 5649

Answers (1)

DrBeza
DrBeza

Reputation: 2251

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?

Upvotes: 10

Related Questions