Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Info on exceptions wanted...

Posted on 2004-10-20
8
Medium Priority
?
183 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
[X]
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
8 Comments
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 400 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

660 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