Re: Exception vs exception

From: Date: Fri, 22 Feb 2008 05:24:46 +0000
Subject: Re: Exception vs exception
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-269839@lists.php.net to get a copy of this message
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/

« previous php.general (#269839) next »