• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Exception.Create() problem in ISAPI Web DLL

Good Morning:
I am currently converting my Delphi Win32 application to ISAPI DLL Web Server Application.

I have done some experiments with Exception.Create function in both projects:

For Delphi Win32 application, Exception.Create() function generate  dialog box with OK button, once i click that OK button, it exits from all the functions/procedures and return nothing.
This is how Exception works ..(see attached code: Part A)

But, for ISAPI DLL Web Server Applications, it return me the "Internal Server Error", i know it is because the dialog box problem ....(see attached code: Part B)

my question is :
is there anyway to use other function instead of Exception.Create() in my DLL project so that wherenever exception is met, my DLL can exit from all the functions/procedures without generating an error?

thanks

Part A:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if TestFunction then
    showmessage('hello');
end;

function TForm1.TestFunction:Boolean;
  procedure RaiseException(const msg : string);
  begin
    raise Exception.Create(msg);
  end;

var
  a : integer;
begin
  result := false;
  a := 1;
  if a = 1 then
    RaiseException('Wrong Data');
  result := true;
end;


Part B:
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  if TestFunction then
    response.content := 'testing completed';
end;

function TWebModule1.TestFunction:Boolean;
  procedure RaiseException(const msg : string);
  begin
    raise Exception.Create(msg);
  end;

var
  a : integer;
begin
  result := false;
  a := 1;
  if a = 1 then
    raiseexception('Wrong Data');
  result := true;
end;
0
chongkeng_woon
Asked:
chongkeng_woon
1 Solution
 
Pierre CorneliusCommented:
You can catch the exception and respond accordingly, e.g.:

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  try
    if TestFunction then
      response.content := 'testing completed';
  except on e: exception do
      response.content:= 'error: '+e.message;
  end;
end;


Another way is to do it without using exceptions, like this:

procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  if TestFunction
    then response.content := 'testing completed'
    else response.content := 'error: test function failed'; //or 'wrong data' or whatever
end;

function TWebModule1.TestFunction:Boolean;
var
  a : integer;
begin
  result := false;
  a := 1;
  result:= (a <> 1);
//  if a = 1 then
//    raiseexception('Wrong Data');
//  result := true;
end;

I prefer the exception method as it is easier to specify numerous problem/messages from a single function.

Regards
Pierre
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now