Solved

Exception handling

Posted on 2003-10-29
13
174 Views
Last Modified: 2010-04-05
Well, in my application I've got some (to keep things simple, let's say) objects: a data manager, a data acquisition module...
Everyone of them has its own exception base class and has to handle the exceptions. Now the question: where is the best place to put the 'try except end'? I don't want to put it around the Application.Run because (as said) every object has to handle its own exceptions. Any opinions?

TIA
0
Comment
Question by:j42
13 Comments
 
LVL 2

Expert Comment

by:xxflip
ID: 9648694
Try except should be used in critacal areas, places where you can't be sure if you're getting the results you need.
Inside the objects events is the place for you try ... except routine, so you would have something like:


procedure SomethingAfterOpen(sender:TObject);
begin

  try
    ...do something
  except
    on e:Exception do {handle exception};
  end;

  ....

  try
    ...do something
  except
    on e:Exception do {handle exception};
  end;

end;

Just keep in mind that if you have several operations inside a try ... except, any one of them can cause an exception, in this case you should analize the exception type to handle it correctly.
0
 
LVL 2

Author Comment

by:j42
ID: 9648787
Hmm...

I will not put the 'try...' in every public method of my objects.
For clarification:
  - I raise the exception in case of a condition I can't go ahead
  - in my 'try...' statement I want to tidy up to be able to go ahead (maybe some additional user input)
Something like 'No floppy in drive A. Continue, ignore or abort?'
0
 
LVL 2

Author Comment

by:j42
ID: 9648803
> Inside the objects events...
I use the MVC pattern, thus the visual stuff is just a thin layer and I don't want it to deal with exceptions.
0
 
LVL 6

Assisted Solution

by:GloomyFriar
GloomyFriar earned 100 total points
ID: 9648990
...
Application.OnException := _FOnAppException;
...

procedure TMainForm._FOnAppException(Sender: TObject; E: Exception);
begin
 try
  ShowMessage('Exception: ' + E.Message);
 except
 end;
end;

...
0
 
LVL 2

Author Comment

by:j42
ID: 9649217
GloomyFriar,

in doing so TMainForm is to be aware of all 'objects' and possible exceptions:

procedure TMainForm._FOnAppExc(...)
begin
  if E is EDataManager then DataManager.OnException(E);
  else if E is EDataAcquisition then DataAcq.OnException(E);
  else if ...
end;

If I add another 'object' (e.g a data filter) I have to remember to extend _FOnAppExc, and I'm short of long term memory ;-)
0
 
LVL 3

Expert Comment

by:Bijith
ID: 9649394
Suppose in your main form unit is the staring point of any particular modele. EG;

TMainForm = class
private
  procedure DoModule1;
  procedure DoModule2;
  procedure DoModule3;
  procedure DoModule4;

public
 .....
end;
.
.

procedure TMainForm .DoModule1;
begin
  try
    Callthe Module1Function
  except
    HandleExceptionForThisModule
    CheckFor the exceptionclass
  end;
end;

procedure TMainForm .DoModule2;
begin
  try
    Callthe Module2Function
  except
    HandleExceptionForThisModule
    CheckFor the exceptionclass
  end;
end;

procedure TMainForm. DoModule3;
begin
  try
    Callthe Module3Function
  except
   HandleExceptionForThisModule
    CheckFor the exceptionclass
  end;

end;

procedureTMainForm. DoModule4;
begin
  try
    Callthe Module4Function
  except
    HandleExceptionForThisModule
    CheckFor the exceptionclass
  end;

end;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:j42
ID: 9649550
That's exactly what I want to avoid ;-)
As long as you have only few modules with few public methods and few visual controls which call the methods this will be a very pracmatic way. But every method of the modules may be called by multiple controls. I also have more than on form for the GUI. My main form is just a container with a lot of panels and I set the parent propetry of the sub forms to these panels.
0
 
LVL 2

Author Comment

by:j42
ID: 9649559
!!!
> every object has to handle its own exceptions
0
 
LVL 5

Accepted Solution

by:
DeerBear earned 100 total points
ID: 9649567
Hi,

From what I read, you're using the Model/View/Controller pattern for your
classes, which is a nice solution.

You could then use an Observer pattern to generate your "error events"
which would be "observed" from appropriate classes.

HTH,

Andrew
0
 
LVL 2

Author Comment

by:j42
ID: 9649628
That sounds good, let me think about it.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9649973
>in doing so TMainForm is to be aware of all 'objects' and possible exceptions:
No. Only the unhandled exceptions.
0
 
LVL 2

Author Comment

by:j42
ID: 9650605
I like to keep this question open a little bit. Maybe there is some more input. Thanks so far.

GloomyFriar,
> every object has to handle its own exceptions
So i like TMainForm to inform the proper object that a certain exception was raised. I don't want to show just a message and die gracefully. In case of an error I want to gather additional information an keep on running by giving control back to the object that raised the exception.

procedure TMainForm._FOnAppExc(...)
begin
 if E is EDataManager then DataManager.OnException(E);
 else if E is EDataAcquisition then DataAcq.OnException(E);
 else if ...
end;
0
 
LVL 2

Author Comment

by:j42
ID: 9656194
I will use the observer pattern to handle my own exceptions and the application event handler to catch everything else.
Thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now