Re: Exception : an ugly grammer in programming
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Wed, 18 Jul 2007 10:21:12 -0400
Yarco wrote:
On Jul 17, 8:16 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:> Erm...I'm thinking about the differences between error table andYarco wrote:Well, friend. Yes, I'm doing PHP. But exception is a traditionalExceptions originally were designed to handle unexpected conditions,
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:
<?php
function fetchPage($url)
{
//...do something
//if the net speed is very slow, cause we can't fetch the page
throw new SlowNetSpeed("page can't be getted");
//if the page is moved
throw new NoPage("page does't exist");
//...do something
}
// so what can i do, if i want the fetchPage procedure go on fetching
when SlowNetSpeed Exception happen?
// it stop when NoPage Exception happen?
while (1)
{
try
{
$c = fetchPage('Some_Where');
// do something
break;
}
catch (SlowNetSpeed $e)
{
}
catch(NoPage $e)
{
break;
}
}
?>
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.
i.e. connection to a database failed. They really weren't designed to
replace normal error processing (i.e. userid/password mismatch).
Normal errors are generally handled by the calling routine, but
unexpected conditions may be handled by the immediate caller - or
several layers higher. Exceptions give the ability to handle the
condition as high in the calling hierarchy as you need to, without
having to pass return codes from each routine in the which has been called.
I still use them in this way, and it works well.
> exception.
> It seems only one difference -- Exception can be caught in high level.
>
> Do you mean we need to use them togather?
>
(Top posting fixed)
I use both - which one depends on what I'm doing. If it's an error which I expect to occur, i.e. "not found" on a database search, incorrect (or missing) data in an entry field, etc., then I return an error code. However, if it's an unexpected condition, i.e. unable to connect to a database which should exist, then I return an exception.
P.S. Please don't top post. Thanks.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- Exception : an ugly grammer in programming
- From: Yarco
- Re: Exception : an ugly grammer in programming
- From: Jerry Stuckle
- Re: Exception : an ugly grammer in programming
- From: Yarco
- Exception : an ugly grammer in programming
- Prev by Date: Re: php extract variable from a string using regexp
- Next by Date: Re: cant insert cyrillic or greek charachters into mssql db
- Previous by thread: Re: Exception : an ugly grammer in programming
- Next by thread: Re: Exception : an ugly grammer in programming
- Index(es):
Relevant Pages
|