Exceptions and builtin functions in PHP5

php.internals

Tumurbaatar S.

22 years ago
----- Original Message ----- From: "Tumurbaatar S." <tumurbaatar@datacom.mn> Newsgroups: php.general Sent: Thursday, April 08, 2004 17:44 Subject: Re: [PHP] Exceptions and builtin functions in PHP5
> > <daniel@electroteque.org> wrote in message > news:49201.203.15.102.65.1081404627.squirrel@www.electroteque.org... > > > If I understand right, PHP5 has an exception > > > handling mechanism but it is only for "manual" using, i.e. > > > a programmer can use try/catch but only for own code. > > > PHP's built-in functions and functions from extensions still > > > use old "return value" method. Yes? > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > I bloody hope not or what is the point ? >
In the current version of PHP, many built-in functions (if not all?) return FALSE on an error and some resource/handle on a success. So, instead of coding like this: ... $res = some_builtin_func(); // func does not raise exception on error if (!$res) throw new Exception(); // so I throw it manually ... I want to write: ... $res = some_builtin_func(); // func raises exception on error // so I don't need to write additional lines ... So will PHP5 (or future versions) work as in my 2nd example?

Derick Rethans

22 years ago
On Thu, 8 Apr 2004, Tumurbaatar S. wrote:
> > news:49201.203.15.102.65.1081404627.squirrel@www.electroteque.org... > > > > If I understand right, PHP5 has an exception > > > > handling mechanism but it is only for "manual" using, i.e. > > > > a programmer can use try/catch but only for own code. > > > > PHP's built-in functions and functions from extensions still > > > > use old "return value" method. Yes? > > > > > > I bloody hope not or what is the point ?
<snip>
> I want to write: > ... > $res = some_builtin_func(); // func raises exception on error > // so I don't need to write additional lines > ... > > So will PHP5 (or future versions) work as in my 2nd example?
No regards, Derick

Tumurbaatar S.

22 years ago
"Derick Rethans" <derick@php.net> wrote in message news:Pine.LNX.4.58.0404081244590.9058@localhost...
> On Thu, 8 Apr 2004, Tumurbaatar S. wrote: > > > > news:49201.203.15.102.65.1081404627.squirrel@www.electroteque.org... > > > > > If I understand right, PHP5 has an exception > > > > > handling mechanism but it is only for "manual" using, i.e. > > > > > a programmer can use try/catch but only for own code. > > > > > PHP's built-in functions and functions from extensions still > > > > > use old "return value" method. Yes? > > > > > > > > I bloody hope not or what is the point ? > > <snip> > > > I want to write: > > ... > > $res = some_builtin_func(); // func raises exception on error > > // so I don't need to write additional lines > > ... > > > > So will PHP5 (or future versions) work as in my 2nd example? > > No > > regards, > Derick
"Zend Engine. Version 2. Feature Overview and Design." PDF says that: ... Compatibility notes No compatibility problems exist, as this feature doesn't exist in previous versions of the scripting engine. In order to simplify error handling in the existing code base, the engine will support a mode in which errors (such as E_WARNING and E_NOTICE) will raise exceptions, instead of displaying an error. This will allow users to use one try..catch statement to recover from any possible errors during the course of a large code block (e.g., establishing a connection to a database server, selecting a database, and issuing a query), without having to add lots of error-handling code. ... Is it not what I'm talking about?

Derick Rethans

22 years ago
On Thu, 8 Apr 2004, Tumurbaatar S. wrote:
> Is it not what I'm talking about?
This document is outdated in much more things then only this. Forget about it. Derick

Ferdinand Beyer

22 years ago
On 8 Apr 2004 at 12:45, Derick Rethans wrote:
> > I want to write: > > ... > > $res = some_builtin_func(); // func raises exception on error > > // so I don't need to write additional lines > > ... > > > > So will PHP5 (or future versions) work as in my 2nd example? > > No
It would be a nice feature to let the user decide whether to throw exceptions when errors are triggered: // either ERROR_TRIGGER or ERROR_EXCEPTION error_handling(ERROR_EXCEPTION); // Don't throw exceptions for notices or warnings error_reporting(E_ERROR); try { mysql_connect() } catch (Notice $e) { echo 'Notice: ' . $e->getMessage(); } catch (Warning $e) { echo 'Warning: ' . $e->getMessage(); } catch (Error $e) { echo 'Error: ' . $e->getMessage(); } catch (Exception $e) { echo 'Unknown exception: ' . $e->getMessage(); }
-- Ferdinand Beyer <fb@fbeyer.com>

Derick Rethans

22 years ago
On Thu, 8 Apr 2004, Ferdinand Beyer wrote:
> On 8 Apr 2004 at 12:45, Derick Rethans wrote: > > > > I want to write: > > > ... > > > $res = some_builtin_func(); // func raises exception on error > > > // so I don't need to write additional lines > > > ... > > > > > > So will PHP5 (or future versions) work as in my 2nd example? > > > > No > > It would be a nice feature to let the user decide whether to throw > exceptions when errors are triggered:
NO! Derick

Ferdinand Beyer

22 years ago
On 8 Apr 2004 at 13:07, Derick Rethans wrote:
> NO! > > Derick
That's all, just 'NO!'? :-P Perhaps this could be done in userland by throwing an exception in a user error_handler... did not test it though...
-- Ferdinand Beyer <fb@fbeyer.com>

Wez Furlong

22 years ago
> On 8 Apr 2004 at 13:07, Derick Rethans wrote: > > NO! > > That's all, just 'NO!'? :-P > > Perhaps this could be done in userland by throwing an exception in a > user error_handler... did not test it though...
"NO!" is a shortcut for "Read the annoyingly long thread about this subject in the archives, please don't start another annoyingly long thread about it again" --Wez.

Ferdinand Beyer

22 years ago
On 8 Apr 2004 at 12:18, Wez Furlong wrote:
> > On 8 Apr 2004 at 13:07, Derick Rethans wrote: > > > NO! > > > > That's all, just 'NO!'? :-P > > "NO!" is a shortcut for "Read the annoyingly long thread about > this subject in the archives, please don't start another annoyingly > long thread about it again"
":-P" is a shortcut for "It's okay for me, it was just a stupid naïve idea, I did not know that it was already discussed in a annoyingly long thread."
-- Ferdinand Beyer <fb@fbeyer.com>

Andi Gutmans

22 years ago
In any case, it'd be interesting to take a look at the possibility of doing this via user-land error handlers. Obviously E_ERRORS wouldn't be recoverable but other errors might work. I'd be happy to hear about your experiences. Andi At 01:41 PM 4/8/2004 +0200, Ferdinand Beyer wrote:

Tumurbaatar S.

22 years ago
> > Perhaps this could be done in userland by throwing an exception in a > user error_handler... did not test it though... >
It seems that works! At least for E_WARNING: class MyException extends Exception { function __construct($errno, $errstr, $errfile, $errline) { parent::__construct($errstr, $errno); $this->file = $errfile; $this->line = $errline; } } function Error2Exception($errno, $errstr, $errfile, $errline) { throw new MyException($errno, $errstr, $errfile, $errline); } $err_msg = 'no exception'; set_error_handler('Error2Exception'); try { $con = pg_connect('host=localhost dbname=test'); } catch (Exception $e) { $err_msg = sprintf('%x - %s', $e->getCode(), $e->getMessage()); }

Marcus Börger

22 years ago
Hello Tumurbaatar, iam curios, why would you want to change the exceptions parameters $file and $line? They are both available through the backtrace at the second element. Try the following: [...] catch (Exception $e) { $trace = $e->getTrace(); var_dump($trace[1]); } That outputs: array(3) { ["file"]=> string(36) "/usr/src/php-cvs/tests/lang/038.phpt" ["line"]=> int(26) ["function"]=> string(10) "pg_connect" } regards marcus Friday, April 9, 2004, 7:42:32 AM, you wrote:
>> >> Perhaps this could be done in userland by throwing an exception in a >> user error_handler... did not test it though... >> > It seems that works! At least for E_WARNING:
> class MyException extends Exception > { > function __construct($errno, $errstr, $errfile, $errline) > { > parent::__construct($errstr, $errno); > $this->file = $errfile; > $this->line = $errline; > } > }
> function Error2Exception($errno, $errstr, $errfile, $errline) > { > throw new MyException($errno, $errstr, $errfile, $errline); > }
> $err_msg = 'no exception'; > set_error_handler('Error2Exception'); > try > { > $con = pg_connect('host=localhost dbname=test'); > } > catch (Exception $e) > { > $err_msg = sprintf('%x - %s', $e->getCode(), $e->getMessage()); > }
-- Best regards, Marcus mailto:helly@php.net

Tumurbaatar S.

22 years ago
Hi, I simply thought that in case of direct using of predefined Exception class, its $file and $line properties would point to my error handler function. I.e. where Exception object was created. Also its constructor does not take file/line as param-s and these properties are read-only, so I thought that there's no way to directly modify them. "Marcus Boerger" <helly@php.net> wrote in message news:13225082703.20040409114359@marcus-boerger.de...