Solved

Info on exceptions wanted...

Posted on 2004-10-20
8
175 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
 
LVL 33

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

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 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 shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

23 Experts available now in Live!

Get 1:1 Help Now