views:

1275

answers:

4

Hello,

I'm asking for Delphi native,not Prism(net).

This is my code:

raise Exception.Create('some test');

Undeclarated idenitifier "Exception".

Where's the problem,how do I throw/raise exceptions?

+4  A: 

You are using SysUtils aren't you? Exception is declared in there IIRC.

Will
+4  A: 

You may need to add sysutils to the uses clause, it is not built in and is optional according to Delphi in a nutshell.

RobS
+22  A: 

The exception class "Exception" is declared in the unit SysUtils. So you must add "SysUtils" to your uses-clause.

uses
  SysUtils;

procedure RaiseMyException;
begin
  raise Exception.Create('Hallo World!');
end;
Andreas Hausladen
For future reference, "undeclared identifier" errors can frequently be solved by searching the included source code for the identifier you're interested in. That will tell you where it's declared, and it might also provide examples of how to use it.
Rob Kennedy
In D2006+ (maybe 2005?) you can use the "Refactoring -> Find Unit" option from the right-click menu to add the required unit to your uses clause.
Gerry
Gerry - fabulous added tip there!... I'm embarrassed to say, I've never noticed that feature. Very cool. : )
Jamo
+1  A: 

Remember to add SYSUTILS to your uses units.

I also suggest you to a nice way to keep track of categories, formats of messagges and meaning of exception:

Type TMyException=class
public
  class procedure RaiseError1(param:integer);
  class procedure RaiseError2(param1,param2:integer);
  class procedure RaiseError3(param:string);
end;

implementation

class procedure TMyException.RaiseError1(param:integer);
begin
  raise Exception.create(format('This is an exception with param %d',[param]));
end;

//declare here other RaiseErrorX

A simple way of using this is:

TMyException.RaiseError1(123);
Marco