?
Solved

Geobul

Posted on 2005-04-27
10
Medium Priority
?
259 Views
Last Modified: 2010-04-05
Hi Folks and\if\or Geobul!

I have been busy with studying exception handling and while walking through replies on questions of mine here at EE I bumped into this answer:

For an exception handling I would use something like:

procedure TForm1.BTNEMO1Click(Sender: TObject);
begin
 try
  emo1 := emo1 + 1;
  ...
  else
      begin
        Raise exception.Create('Fout opgetreden');         end;
  end; // end of case statement
 except
   // do something here with the exception like
   on E: Exception do ...
 end;
end;

Now I'd like to know what there is to do with the exception? Furthermore > if I use the Raise Exception.Create('') then some messagebox pops up asking the user whether to inform me. Clicking yes automatically kicks Outlook out of its lazy chair and fills in the unit name, procedure name and linenumber which causes the headache. Now I assume that that is what you meant  by doing something with the exception, right?

Regards Peter

Ps I'm using this codefragment so I'd like to understand it as much as possible.
0
Comment
Question by:PeterdeB
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:StevenB
ID: 13882924
I'm not sure exactly what your question is, but in my opinion it is bad practice to use exceptions to control program flow in the fashion you are describing.
0
 

Author Comment

by:PeterdeB
ID: 13882978
Hi StevenB,

Well I'm all ears and would be more than happy to learn about alternatives or other approaches. Geobul just responded to a question of mine in which I had implemented incorrect exceptionhandling. That was part of my question, I asked how I could make sure I'd catch any exceptions that may occur so he among others replied.

Regards,

Peter
0
 
LVL 4

Accepted Solution

by:
StevenB earned 200 total points
ID: 13883178
Well, what you seem to be try ing to do is to direct the flow of control within a function/procedure by raising an exception within that procedure:

procedure DoSomething;
begin
  try
    // Do Something
    // If a problem Doing Something then throw an exception
  except
     // Handle the exception, perhaps by showing a message dialog
  end;
end;

In my opinion it is not good practice to handle the exception thrown by a function in itself. If you want to show a message within a function if there is a problem then you can do it more simply like this:

procedure DoSomething;
begin
  // Do Something
  // If a problem Doing Something show a message dialog
end;

Where exceptions are useful is when you do not want the function to handle the problem situations, but want to pass the responsibility for handling the problem to the calling function:

Procedure DoTask;
begin
  try
    DoSomething;
  except
    // Handle the exception during DoSomething, perhaps by showing a message dialog
  end;
end;
 
procedure DoSomething;
begin
  // Do Something
  // If a problem Doing Something then throw an exception
end;

The advantage is that DoSomething is likely to be a utility function that may be used by many different processes within a single or many applications. You may want to handle the exception differently in different situations, depending on the context of what you're trying to do. As a general rule I believe it is best to handle exceptions in the outermost layer of nested calls that is appropriate, often at an application specific level.

I hope that made some sort of sense and you can see what I'm getting at.

Steven
0
Technology Partners: 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!

 

Author Comment

by:PeterdeB
ID: 13883645
Hi Steven!

After reading 4 times what you posted I think I get the message. Nevertheless your approach raises a question (speaking of raising exceptions :)

I agree on the messagebox being a suitable solution for all kinds of problems and I use it as much as I can. I use it when I need some text from some file > to respond properly when the file may be missing for instance, and so on.

But isn't the use of the messagebox limited to cases in which you know what could be going wrong in advance?

Regards,

Peter
0
 
LVL 2

Expert Comment

by:Ratje
ID: 13884040
Take a look here:
    http://www.madshi.net/

MadExcept might be of some help.
Some more info here:
http://help.madshi.net/madExcept.htm

Groeten
Ratje
0
 

Author Comment

by:PeterdeB
ID: 13884425
Hi Ratje!

May I ask what exception handling you use or can I assume you use Madshi's?

Regards,

Peter

Is het bij jullie ook zo'n heeeeeerlijk weeeeer als hier?? :)
0
 
LVL 2

Expert Comment

by:Ratje
ID: 13884451
Madshi...love that tool :)

PS: Ja zaaaalig weer hier, ga zo maar ff buiten een peukje roken denk ik :)
0
 

Author Comment

by:PeterdeB
ID: 13884744
Oh okay.....I used Eurekalog for some time and now I use TExceptionManager component because I want to display each and every messagebox or dialog in the same style as my main form, which is skinned-to-da-max :)

Om de een of andere reden lijkt het zo vanzelfsprekend dat de rest niet snapt wat wij schrijven terwijl het aan de andere kant eigenlijk maar moeilijk voor te stellen is dat dit abracadabra zou zijn voor de meesten.....:)
0

Featured Post

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.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

621 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