Re: Exception vs exception

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




But it's give a many information using $e->getTrace()
is this correct. If there are several mysql_query then
I can put it as bunch within try block and catch
exception easily.

No, it doesn't give any such information.

1 :<?php
2 :mysql_connect('server', 'username', 'password');
3 :mysql_select_db('database_name');
4 :
5 :try{
6 :        $result = mysql_query('SELECT * from unknowntable');
7 :        echo __LINE__ . "\n";
8 :}catch(exception $e){
9 :        echo __LINE__ . "\n";
10:}

Line numbers added for effect.

It will print the line number in the "try".

It never prints the line number in the "catch".

$ php -f ./test.php
7

In second approach for every query I have to write
throw new MySQLException("My Message"). It's very time
consuming isn't it?

Use a database abstraction layer and run everything through that or simply use a function.


function Query($query='')
{
        if (!$query) {
                throw new Exception("No query passed to Query method");
        }

        $result = mysql_query($query);
        if (!$result) {
                throw new Exception("Query " . $query . " is invalid.");
        }
        return $result;
}

Then simply:

$myquery = "select * from table_that_exists";
try {
        $result = Query($myquery);
	echo "Got result \n";
} catch (Exception $e) {
        echo $e->getMessage() . "\n";
        // ... do whatever here.
}

$myquery = "select * from table_that_doesnt_exist";
try {
        $result = Query($myquery);
        echo "Got result\n";
} catch (Exception $e) {
        echo $e->getMessage() . "\n";
        // ... do whatever here.
}



--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux