I'm trying to catch an Exception which may be thrown in a class constructor, but i can't figure why it is not working.
Here my code:
File #1
<?php
use Exception;
class MyClass {
function __construct($x) {
if($x < 1) { echo "ok";}
else {
throw new Exception ("ERROR!"); //throws error
}
}
}
File #2
<?php
include 'file1.php'
try {
$y = new MyClass(3);
} catch (Exception $e) {
echo $e->getMessage();
}
Now I expect to see a printed line telling me "ERROR!". Instead I get a
PHP Fatal error: Uncaught exception 'Exception' with message 'ERROR!' in file1.php:7
Stack trace:
#0 file2(4):MyClass->__construct(3)
#1 {main}
thrown in file1.php on line 7
Any idea on what can it depend on?