?
Solved

Exception handling

Posted on 2003-10-29
13
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Assisted Solution

by:GloomyFriar
GloomyFriar earned 400 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
 
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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

650 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