Board index » delphi » Re: Hejlsberg on language design

Re: Hejlsberg on language design


2003-10-12 02:10:55 AM
delphi32
Ingvar Nilsen writes:
Quote
>
>I see *far* too much improper use of exceptions in Delphi code and
>that code is not written only by newbies.

Hm.. interesting!
Do you mind give just a brief example? ;-)
One that Anders mentions in the article:
try
// do something
except
end;
There may be a very rare exceptions <g>where this (suppression without
action) is justified, for example when dealing with some external process
you have no control over. But even then, chances are there is *something*
that should be done within such an exception. Also, as Anders points out,
exceptions are often handled at a local area when they should be allowed to
percolate up, in most cases at least, to a higher level, centralized,
exception handler.
try
// do something
SomeResource.Free;
except
SomeResource.Free;
end;
... or even ...
try
// do something
success := True;
except
SomeResource.Free;
Exit;
end;
if success then
SomeResource.Free;
I saw stuff like this all over a project I took over, recently written by a
company claiming to have Delphi expertise since D1. There should be no
exception blocks here, only finally blocks.
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson
 
 

Re: Hejlsberg on language design

Wayne Niddery [TeamB] writes:
Quote
There may be a very rare exceptions <g>
LOL!
Quote
But even then, chances are there is *something* that should be done
within such an exception.
Agreed. Actually, my first code in Delphi may have looked like that, but
not anymore.
Quote
Also, as Anders points out, exceptions are often handled at a local
area when they should be allowed to percolate up, in most cases at
least, to a higher level, centralized, exception handler.
Hm.. thinking..thinking..<g>
That depends on what "handled" in this context means.
Resources allocated in the function block definitely need to be taken
care of by the local exception handler.
Quote
if success then
Wow - looks like my code! I use "Success" Booleans a lot!
--
Ingvar Nilsen
 

Re: Hejlsberg on language design

Ingvar Nilsen writes:
Quote

Hm.. interesting!
Do you mind give just a brief example? ;-)
I have several examples, this one is quite common. ^_^
function IsNumeric (const s : string) : boolean;
begin
try
Result := true;
IntToStr (s);
except
Result := false;
end;
end;
Wien.
 

Re: Hejlsberg on language design

Erwien Saputra writes:
Quote
I have several examples, this one is quite common. ^_^

function IsNumeric (const s : string) : boolean;
begin
try
Result := true;
IntToStr (s);
except
Result := false;
end;
end;
And..?
--
Ingvar Nilsen
 

Re: Hejlsberg on language design

"Peter Sleuth" wrote
Quote

www.artima.com/intv/nonvirtual.html
I was particularly ingratiated to read the following statement by Anders:
"But that said, there's certainly tremendous value in knowing what
exceptions can get thrown, and having some sort of tool that checks. [...] I
think we can certainly do a lot with analysis tools that detect suspicious
code, including uncaught exceptions, and points out those potential holes to
you".
Now go have a long hard look at QC #1556,
"Design-time utility to track exception propagation paths".
And please up your ratings, Hejlsberg (the father of Delphi) condones it
<g>!!!
Kristofer
 

Re: Hejlsberg on language design

Ingvar Nilsen writes:
Quote

And..?
I am not really sure what do you mean. Are you asking for other
examples, or why that code is bad?
If you are asking for other examples, another that I have in mind:
repeat
SomeVar := false;
try
qrySomething.Post;
SomeVar := true;
except
qrySomething.Cancel;
end;
until SomeVar = true;
I think those are example of abusing exception.
Wien.
 

Re: Hejlsberg on language design

"Rudy Velthuis (TeamB)" <XXXX@XXXXX.COM>writes
Quote
Oliver Townshend writes:

>I can not find anything for Delphi 5 and 7 except for those you mentioned.

AFAIK, the only version that had no language changes was D5. Delphi 7 didn't
have a lot.
--
Did Delphi 7 have any language changes at all? If so, what were they?
Chris Burrows
CFB Software
www.cfbsoftware.com
 

Re: Hejlsberg on language design

Quote
If you are asking for other examples, another that I have in mind:

repeat
SomeVar := false;
try
qrySomething.Post;
SomeVar := true;
except
qrySomething.Cancel;
end;
until SomeVar = true;

I think those are example of abusing exception.
Except that we are talking here about Procedures which don't return a value,
except that they do, the value in the exception. Life would be a bit
simpler if Post returned an error status one could use, otherwise using
exceptions to capture it is the only thing we can do. Your previous example
was in some ways a poor way to test for a number, but in other ways a good
way, since it ensured that the function conformed to the definition of the
IntToStr function, without attempting to duplicate it (or even worse,
failing to duplicate it).
Oliver Townshend
 

Re: Hejlsberg on language design

Ingvar Nilsen writes:
Quote

Agreed. Actually, my first code in Delphi may have looked like that, but
not anymore.
Oh, certainly - me too. It was a new concept and took awhile to learn its
proper use. But it seems many never learn it.
Quote
>Also, as Anders points out, exceptions are often handled at a local
>area when they should be allowed to percolate up, in most cases at
>least, to a higher level, centralized, exception handler.

Hm.. thinking..thinking..<g>
That depends on what "handled" in this context means.
Resources allocated in the function block definitely need to be taken
care of by the local exception handler.
That's what *finally* blocks are for. Exceptions are not intended to be used
for cleaning up resources. In most cases you don't need an exception block
at all, just a finally:
sl := TSomeObject.Create;
try
// do stuff that may cause exception
finally
sl.Free;
end;
The allocated object will always be properly freed whether there is an
exception or not.
Even if you handle the some or all exceptions here, it should be:
sl := TSomeObject.Create;
try
try
// do stuff that may cause exception
except
on e:SomeException do
begin
// do something about exception
end;
end;
finally
sl.Free;
end;
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson
 

Re: Hejlsberg on language design

Ingvar Nilsen writes:
Quote
>
>function IsNumeric (const s : string) : boolean;
>begin
>try
>Result := true;
>IntToStr (s);
^ Should've been StrToInt here
Quote
>except
>Result := false;
>end;
>end;
And..?
Creating exceptions is expensive, the compiler adds a lot of code to handle
this, it takes time and resources. In this example, you can use the good old
Pascal Val function and check the error code (that's actually what StrToInt
does anyway).
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson
 

Re: Hejlsberg on language design

Quote
Even if you handle the some or all exceptions here, it should be:

sl := TSomeObject.Create;
try
try
...
except
finally
sl.Free;
end;

Oh, man, it seems you wanna provoke yet another "try.. except finally" thread 8-o
 

Re: Hejlsberg on language design

Oliver Townshend writes:
Quote
Except that we are talking here about Procedures which don't return a
value, except that they do, the value in the exception. Life would
be a bit simpler if Post returned an error status one could use,
otherwise using exceptions to capture it is the only thing we can do.
Datasets have events for handling errors, and generally if the first post
fails, then repeating it like in this example isn't going to help.
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson
 

Re: Hejlsberg on language design

Quote
repeat
SomeVar := false;
try
qrySomething.Post;
SomeVar := true;
except
qrySomething.Cancel;
end;
until SomeVar = true;
And..?
 

Re: Hejlsberg on language design

"Ingvar Nilsen" <XXXX@XXXXX.COM>writes
Quote
Erwien Saputra writes:
>I have several examples, this one is quite common. ^_^
>
>function IsNumeric (const s : string) : boolean;
>begin
>try
>Result := true;
>IntToStr (s);
>except
>Result := false;
>end;
>end;


And..?

In this exampe, "the programmer" is using exceptions to do routine
validation. This is lazy coding. If you want drive me ape-shit, just make
me debug somebody elses app where an exception is thrown constantly in some
kind of iteration loop, where the above example is THE RULE not the
EXCEPTION.
Its lazy. Exceptions should not be thrown as an expected outcome of what is
actually validation logic...
 

Re: Hejlsberg on language design

Quote
Now go have a long hard look at QC #1556,
"Design-time utility to track exception propagation paths".
add the citation to the report