the problem with comparing Java to PHP is they are, for a start, almost completely different.
Everything in Java is an object, or static class.
When reading the man pages for Java classes, such as InputStreamReader, you will see the line "throws IOException".
If you look at the source of the InputStreamReader class, you will notice a line:
so you will need to
catch the IOException and handle accordingly. Normally with a "file not found" error message or such.
In PHP, this is a different case, as the throw-catch has not been available to PHP developers as long as it has in Java, thus hardly any functions or methods available to us make use of it.
For now atleast, it may seem tedious to do it this way.. but in the long run I think it is a much better way of handling errors, or rather, handling errors in an OO fashion.
Last year I wrote my first PHP class that utilises the throw-catch methodology - funnily enough it is a MySQL DB class - which is probably the first class every PHP developer writes

- I was using lines such as:
Code: Select all
<?php
class MySQLException extends Exception {}
class MySQLDB
{
function DoSomething ()
{
if (!mysql_connect($blah, $blah, $blah)) {
throw new MySQLException('Cannae connect to DB Cap\'in!');
}
}
}
try {
MySQLDB::DoSomething();
} catch (MySQLException $e) {
echo $e->getMessage(); //output's Cannae connect to DB Cap'in! and continues
} catch (Exception $e) {
die('Fatal Error: ' . $e->getMessage()); //dies
}
?>
Now, the nice bit about this method of error handling is I can specify in my doc's/comments that my method DoSomething throws a MySQLException - which is a bit more specific than just a generic E_USER_WARNING or such.
--
The difference between throw/catch and PHP's function trigger_error() is that the method of detecting what type/level of error is not based upon what arbitrary argument you provide a function, but on what type of error has been thrown.
This is slowly but surely happening in PHP5+.
From Zend's PHP-101:
Code: Select all
try {
execute this block
}
catch (exception type 1) {
execute this block to resolve exception type 1
}
catch (exception type 2) {
execute this block to resolve exception type 2
}
... and so on ...