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
Solved

Info on exceptions wanted...

Posted on 2004-10-20
8
179 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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