Solved

Info on exceptions wanted...

Posted on 2004-10-20
8
180 Views
Last Modified: 2012-03-15
Ok, the following is a basic example to illustrate my question (so don't look at the code for errors):

function X:boolean;
begin
 
try
  {some code}
  {result:=value of whatever above code found}
except
 {a return value of -1 to show function failed}
end;

What i want to do is have the function be able to catch the exception in {some code} and "eat it" so it goes away - ie: the "object" is handled, dead, never existed, gone to the big exception heaven in the land of delphi, etc.

I know "eating" an exception is bad - but since the function return value is already set to show an error which i want to handle in my own way, i thought it would remove the exception object and leave me to get on with it.

Since i surround the block of {some code} with a try...except i thought i was handling the exception object - ie: making it go away and remove itself from where-ever the exception object is (the stack?).

But since the ability to re-raise the exception exists it must still be hanging around like a bad smell somewhere.


So, does anyone know how to kill off an exception so it never happended - ie: how they do it in the Val procedure of Delphi?



0
Comment
Question by:mi6agent
8 Comments
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 100 total points
ID: 12359962
Actually, to handle an exception silently, just don't reraise it...

try
  try
    {some code}
  except
    {Ignore it.}
  end;
  {result:=value of whatever above code found}
except
 {a return value of -1 to show function failed}
end;

Eating exceptions isn't bad. It just means something went wrong and your code must do something to make it right. If you try to divide by zero in a function that should return the result of this division then you just catch the exception and set some value that should be returned in case of this error.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12360113
Hi,

I think that an exception can be re-raised in the except part only:
---
result := 0;
try
  {some code}
  {result:=value of whatever above code found}
except
 {a return value of -1 to show function failed}
 raise; // re-raises the exception
end;
raise; // this one won't work
---

Once the execution of try..except has finished the exception cannot be raised again (excluding last WinAPI exception).

Regards, geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 12360179
BTW. perhaps Val procedure simply doesn't produce any exception. This can be done by parsing S parameter first and if parser shows Ok convert it to V without an exception.
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 34

Expert Comment

by:Slick812
ID: 12362783
????
your code may work? ? ?
If I remember right, , , , If you are running your program in the Delphi  VCL, then it is in the Debug, and even if you have that   try   except and do not reRaise the exception,, you WILL GET  the exeption message anyway, and your code progression will STOP at the exception (for Debugging), , , , I  think would try and run that code out side the delphi VCL and see what happens, it seems to me there will not be a exception message for that try   except  block?. . .
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12366260
if you are running from inside the IDE, goto (menu)
Tools | Debugger Options, Language Exceptions

uncheck the "Stop on Delphi Exceptions"

and Delphi won't stop in the exceptions any more

how do they do it in the VAL procedure?... they just never generate an exception... (they're not using StrToInt or anything like that)
they go through the string, checking if is a valid number, if it is, it is returned in the result variable, if is not, the offending character position is returned in the Code
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12366951
first of all, write your function like this:

function X:boolean;
begin  
  Result := False; // initialize before try..except
  try
    {some code}
    Result := True;  
  except
    on E: Exception do
      begin
        {Do what you want on Exception}
        Exit; {and Exit}
        {here you you can raise exeption if you want, just remove the exit from above}
        raise EConvertError.Create('Your exception message here');
      end;
  end;
end;

After that... Go to Tools / Debugger Options / OS Exceptions and untick "Stop on Delphi Exceptions"
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 12367075
>> But since the ability to re-raise the exception exists it must still be hanging around like a bad smell somewhere.

It's hanging around as long as you're in the except block. If you leave the except block without having reraised the exception, "System.DoneExcept" is called (see assembler code in CPU view) and "DoneExcept" calls "ExceptObject.Free".
0
 

Author Comment

by:mi6agent
ID: 12516689
thanks guys, i think i understand it all a little bit better now :)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

713 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