Hi

Hi,

I am having some issue with my Delphi code in android. I am trying to save some changes to the SQLite database from my app. I have created record object for my tables and have a generic datamanager for handling all database calls. The issue I am facing is if any exception is raised within the datamanager object then android hangs and doesn't even show me the error message. I have given below snippet of my datamanager code and also two sets of code one which works properly and the other which causes the issue. 

The error is raised by the validate procedure, which validates the data integrity and raises an exception if there are any inconsistencies.

Code snippet from the datamanager for saving the record to the database.
function TData_Manager.Save_Record(const ARec: T): Boolean;
begin
  { start a new transaction }
  _ConMgr.Start_Transaction;
  try
    { Validate changes before saving }
    FClass_Map.Validate(ARec);
    { Save changes to database including any detail items }
    FClass_Map.Save_ToDatabase(ARec);
    { Commit Changes }
    _ConMgr.Commit_Transaction;
    Result := True;
  except
    on e: Exception
    do begin
       { Rollback changes }
       _ConMgr.Rollback_Transaction;
       raise;
    end;
  end;
end;

ERROR CODE
**************************************
The following code doesn't even show me the message, but when run in debug mode it gives me segmentation error
function Save_Changes: Boolean;
begin
  try
    Result := FData_Manager.Save_Record(FData);
  except
    on e: Exception
    do begin
       Showmessage(e.message);
       Result := False;
    end;
  end;
end;
**************************************

WORKING CODE
**************************************
The following code works fine and gives me a proper error message and android doesn't hangs.
function Save_Changes: Boolean;
begin
  try
    if FData.Validate
    then Result := FData_Manager.Save_Record(FData);
  except
    on e: Exception
    do begin
       Showmessage(e.message);
       Result := False;
    end;
  end;
end;

Can anyone suggest me a solution for this. I am not sure why an error raised in a generic class gives a segmentation error in android. 

Thanks

Comments

  1. I think you should not generate an exception if data is invalid. That's a sneaky flavour of using exceptions for control flow. It does not LOOK like one, but it is :-)

    ReplyDelete
  2. I can take out the validation part but it can still raise a database exception which also has the same problem.

    ReplyDelete
  3. Oh yes absolutely, database exceptions are allowed ;-) I don't have XE6/7 but in a case like my first option would be to handle the exception internally and create a TOperationResult record with a "Succeded" flag and a message, then return that. It's not ideal, but I think it's an easy way out of trouble.

    ReplyDelete
  4. I was thinking of doing that if there was no other solution. I think it won't be 100% of all the exceptions but should take care of most of it. Was trying to find any reason why it was happening.

    ReplyDelete

Post a Comment