[DUG] What is the difference by using following two methods?

Conor Boyd Conor.Boyd at trimble.co.nz
Tue Aug 7 10:39:43 NZST 2007


I'm not aware of any difference in the real world.

>From the help on try..except statements (in D7, 'cos I can't bring
myself to try looking for it in D2006 Help), here are some relevant
snippets:
"	The syntax of a try...except statement is
		try statements except exceptionBlock end
	where statements is a sequence of statements (delimited by
semicolons) and exceptionBlock is either
		another sequence of statements or
		a sequence of exception handlers,"

So for a start, you can put any statements you like in the except
section.

Then:
"	An exception handler has the form
		on identifier: type do statement
	where identifier: is optional (if included, identifier can be
any valid identifier),
	type is a type used to represent exceptions, and statement is
any statement."

"Type" can be any valid class, which can thus be TObject.

Now one would argue that it's not much use as a TObject, since in
reality it is an Exception or one of it's descendants, which is why
convention dictates that we write it as an Exception or whatever
particular exception type we're interested in.

But there's nothing in the help that says we have to declare it as an
Exception.  I would expect the behaviour for the two examples you have
given to be identical, unless you have some crazy code which explicitly
raises an exception of a type which doesn't inherit from the Exception
class.  And by crazy I mean something like:
	raise TObject.Create;
Which does compile. ;-)

Always using Exception or it's descendants is just convention.

Cheers,

C.

-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz]
On Behalf Of Leigh Wanstead

Good morning,

May I ask what is the difference by using following two methods?

  try
    ...
  except
    on Error: TObject do
    begin
	...
    end;

  try
    ...
  except
    on E: Exception do
    begin
	...
    end;

Why can TObject be an object in try except block?



More information about the Delphi mailing list