Solved

Exception handling

Posted on 2003-10-29
13
177 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 188
Delphi 2 77
Not able to call Delphi XE10 dll function from Delphi 6.0 4 49
migrate this code to work on android 1 34
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…
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…

730 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