Jump to content
The Uniform Server Community

try-catch is not working propery...


Recommended Posts



I am trying out a very simple code snippet - Example #1 from this page: http://php.net/manual/en/language.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...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...