Exception bubbling issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
landshark
Forum Newbie
Posts: 5
Joined: Sat Jan 07, 2012 10:34 am

Exception bubbling issue

Post by landshark »

Hello -

I'm having trouble understanding an exception bubbling issue.

My code (stripped down to its essence):

Code: Select all

my_func($org_id, $dbh) {
	try {
		$queryiefe = "insert into another_table (id, error_type_id) values (?, 1)";

		$q = $dbh->prepare($queryiefe);
		$q->bindParam(1, $org_id);
		$q->execute();
	}
	catch(PDOException $pe) {
		if( $pe->getCode() != 23000)
		{
			echo "Error insert event_feed_error\n";
		}
		else {
			echo "Ignoring duplicate key error for $org_id\n";					
		}
	}
}


$dbh = new PDO($dbconn, DB_USER, DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$querysef = "select * from driving_table";
$q = $dbh->query($querysef);
$q->setFetchMode(PDO::FETCH_ASSOC);

while ($rowsef = $q->fetch()) {
	my_func($org_id, $dbh);
}
The while loop executes 49 times. The first four executions of my_func() result in unique key violations, which are caught by the catch(PDOException $pe), as evidenced by the echo statement. This is followed by 45 iterations without exceptions.

After 49 loop iterations, the fetch() statement comes up empty. At that point, I get the following message, pointing at the while(fetch()) line:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3822-1' for key 'PRIMARY''

This is the error (23000) that I'm catching in my_func(). The key value 3822-1 indicates that it is the last of the four unique key violations.

Why is this bubbling up to main?

Thanks!
landshark
Forum Newbie
Posts: 5
Joined: Sat Jan 07, 2012 10:34 am

Re: Exception bubbling issue

Post by landshark »

I've even tried wrapping the call to my_func() in its own try with a generic catch, but I'm still getting the same fatal error.

Code: Select all

try {
	my_func($org_id, $dbh);
}
catch (Exception $e) {
        echo "uncaught exception of type: ".gettype($e)."\n";
}
Am I fundamentally misunderstanding something here about exception handling?

Thanks
Post Reply