Solved

Exception handling

Posted on 2003-10-29
13
173 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

13 Experts available now in Live!

Get 1:1 Help Now