Jump to content


try-catch is not working propery...

  • Please log in to reply
No replies to this topic

#1 s7orm



  • Member
  • Pip
  • 1 posts
  • Main OS: Windows Vista

Posted 31 January 2010 - 02:24 PM


I am trying out a very simple code snippet - Example #1 from this page: http://php.net/manua....exceptions.php

this code is as follows:
function inverse($x) {
	if (!$x) {
		throw new Exception('Division by zero.');
	else return 1/$x;

try {
	echo inverse(5) . "\n";
	echo inverse(0) . "\n";
} catch (Exception $e) {
	echo 'Caught exception: ',  $e->getMessage(), "\n";

// Continue execution
echo 'Hello World';

When opening the test file in my browser instead of the desired output I get:
Fatal error: Uncaught exception 'Exception' with message 'Division by zero.' in W:\www\test.php:4 Stack trace: #0 W:\www\test.php(11): inverse(0) #1 {main} thrown in W:\www\test.php on line 4

However if I run the code from the console, everything works fine... do you have any ideas what went wrong there?
php -f w:\www\test.php

PS. Some additional information - I have UniServer 3.5 and today I tried out the 5.5 version, however there were some problems with it (exception when starting the service), so I deleted the new one and continued using UniServer 3.5. Also at some point I had the intention of integrating PHP with IIS, so I installed PHP on my computer, but I deinstalled it in the meantime...

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users