Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Info on exceptions wanted...

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
mi6agent
Asked:
mi6agent
2 Solutions
 
Wim ten BrinkSelf-employed developerCommented:
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
 
geobulCommented:
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
 
geobulCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Slick812Commented:
????
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
 
BlackTigerXCommented:
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
 
Ivanov_GCommented:
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
 
MadshiCommented:
>> 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
 
mi6agentAuthor Commented:
thanks guys, i think i understand it all a little bit better now :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now