Posted by Joe Scylla on 07/18/07 15:59
Yarco wrote:
> Well, friend. Yes, I'm doing PHP. But exception is a traditional
> machinism in programming languages. I'm very doubt about this.
> In procedure language, we return error code when we meet an error. So
> we need to design error code table and judge them in several
> statements. Finally they become boring.
> But how about use exception in OOP? Did we drop all "return error
> code" styles?
> If i want to fetch a webpage:
.... snipped...
> It is very ugly if there exists more Exceptions and other procedure.
>
> Then i'm missing "return error code" style. But i know "return error
> code" is not a good method when we deal with error. And exception is
> also an ugly grammer when we want to keep code clean.
Exceptions don't replace conventional error management. On the most
cases i only use exceptions on function/method scope and return FALSE is
a Exception got thrown. In the most cases it's not relevant why the
operation failed for the caller.
<code>
function parse_file($file)
{
try
{
if (!@file_exists($file))
throw new Exception("File does not exist");
if (!$content = @file_get_contents($file))
throw new Exception("Error reading file!");
//do something with content
return $content;
}
catch (Exception $e)
{
//do something with your Exception
return false; //or a error code or a default value
}
}
</code>
I'm using a own Exception class fetching the system error message
(error_get_last, mysql_error, ...) and writing a log file and have a
LastError/LastErrors property for fetching the last error message.
[Back to original message]
|