Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with TRY...EXCEPT...END; statement

Status
Not open for further replies.

TheBugSlayer

Programmer
Sep 22, 2002
887
US
Hello all. Please help me understand the try except statement.
My program is structured like this:
Code:
while not RS.EOF do begin
   try
	try
	  ItemList.LoadFromFile(SomeFile);
        except
          RS.MoveNext;
          continue;
        end;
        ...
        ...
        ...
        ...
   except
       ...
       LogError(Msg);
       RS.MoveNext;
       continue;
   end;
end;

What I am trying to achieve is to take the corresponding action when an exception occurs but still be able to move on to the next record in the recordset and process it. However, I leave the program running overnight and when I come in the morning it stops at the first exception. I would have hoped it records the error but continues processing. How do I achieve that?

Noe: The statements in the outer TRY EXCEPT block my generate exceptions too...

Your helping is appreciated.
 
Change the outer one to Finally

while not RS.EOF do
begin
try
try
ItemList.LoadFromFile(SomeFile);
except
LogError(Msg);
end;
...
finally
...

RS.MoveNext;
continue;
end;
end;

 
Oh, I see. Finally will move to the next record no matter what, correct? I'll test it and post the result here...
 
You cannot break, continue or exit out of a FINALLY clause...Any other ideas?
 
beware that with try..finally loops, the exception is not handled at all, this means that if there is an exception in the try section, the finally clause will be executed and the exception will raised after the finally..end section. also beware that exceptions raised (when using try..except..end) in the except..end section are not handled.
so your code should be like this :
Code:
while not RS.EOF do begin
    try
     ActionSuccesFull:=False; 
     ItemList.LoadFromFile(SomeFile);// could generate exception
     ActionSuccesFull:=True; 
    except
      on E : Exception do
       begin
        LogError(E.Message);
       end;
     end;
   if not ActionSuccessFull then
    begin
     //take correct actions
    end;
.....// do stuff   
    try
     RS.MoveNext; // could generate exception
    except
      on E : Exception do
       begin
        LogError(E.Message);
       end;
     end;
        ...
        ...
        ...
        ...
end;



--------------------------------------
What You See Is What You Get
 
Thanks whosrdaddy.
The thing is I want to move to the next record regardless or whether there is an error. So, modifying your speudo, mine would be like:
Code:
while not RS.EOF do begin
  try
    try
     ActionSuccesFull:=False; 
     ItemList.LoadFromFile(SomeFile);       ActionSuccesFull:=True; 
    except
      on E : Exception do
       begin
        LogError(E.Message);
       end;
     end;
   if not ActionSuccessFull then
    begin
     RS.MoveNext;
     Continue;
    end;
.....// do stuff   
       
    except
      on E : Exception do
       begin
        LogError(E.Message);
        RS.MoveNext;
        Continue;
       end;
     end;
        ...
        ...
        ...
        ...
     RS.MoveNext;//There is no exception, move on.
end;

Do you think that is alright? I will test and let you know...Thanks again.
 
I don't understand why you need two (2) try..except blocks.
Code:
while not RS.EOF do 
begin
   try
       ItemList.LoadFromFile(SomeFile);

//          do other things

   except
//        don't care what went wrong
   end
   RS.MoveNext;
end;

Cheers
 
that's because the MoveNext method could also throw an exception (under certain circumstances). It's up to the programmer to decide to deal with it or not...

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top