Recent

Author Topic: How to catch an exception raised within a thread ?  (Read 7193 times)

swierzbicki

  • Full Member
  • ***
  • Posts: 177
How to catch an exception raised within a thread ?
« on: March 26, 2014, 08:35:25 pm »
Hello,

I'm sending data to a remote server thanks a worker thread.
My thread is basically doing this :

Code: [Select]
while not terminated do
begin
sleep a while

try

Connecting to local SQLite Database
Connection to remote Database

Opening Local Table
Sending Local data to remote server
Updating local table
Applying update to remote table
Applying update to local table

Commiting remote data
Commiting local data

Closing Local Table
Closing Local connection
Closing Remote Connection

except
synchronizing and logging error message
end;
end;

Issue arise when data connection is lost (wifi hotspot is too far from the device) during the remote apply update !
An exception is raised despite the try except end block ! : user is getting an error message telling that the software becomes unstable.... Pressing OK or CANCEL leads to application crash. Exception is about TPQConnection (PostgreSQL) prepared query failed....

How can I avoid getting this exception ?

Ps : I'm running my software Under Windows Mobile 6.5.3. Local database is SQLite. Remote Database is PostGreSQL. Thread is starting from a Datamodule.
Application.CloseOnException is set to False....
Lazarus 1.6.2
fpc 3.0.0
wince/win32/win64
delphi berlin

parcel

  • Full Member
  • ***
  • Posts: 143
Re: How to catch an exception raised within a thread ?
« Reply #1 on: March 27, 2014, 01:48:29 am »
Most DB controls may not be thread-safe.

How about use critical section or similar method at thread?

swierzbicki

  • Full Member
  • ***
  • Posts: 177
Re: How to catch an exception raised within a thread ?
« Reply #2 on: March 27, 2014, 07:52:51 am »
Hi Parcel,

My TPQConnection, Transaction and query are created at runime in the thread. They don't communicate with the main form / datamodule.
My issue is more how to catch the exception ! Working with critical section will change that.

I've never used critical sections. Where should I create my object ? befreo creating the thread ? In the thread initialization (oncreate event) ?
Lazarus 1.6.2
fpc 3.0.0
wince/win32/win64
delphi berlin

BlueIcaro

  • Hero Member
  • *****
  • Posts: 793
    • Blog personal
Re: How to catch an exception raised within a thread ?
« Reply #3 on: March 27, 2014, 11:08:24 am »
Hi, a idea, I don't tested.
In your thread code, catch de exception, and then call the Synchronize to invoke a event in your main thread.

In the wiki, you have more info: http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial

/BlueIcaro

snorkel

  • Hero Member
  • *****
  • Posts: 817
Re: How to catch an exception raised within a thread ?
« Reply #4 on: June 27, 2014, 10:16:12 pm »
handle the exception in the thread just as in the main process and then use messages to send the exception message and error code back to the main thread for logging or whatever.

All the platforms support windows style messages.  i.e. use the postmessage

http://lazarus-dev.blogspot.com/2008/01/new-0926-features-part-1-sendmessage.html
***Snorkel***
If I forget, I always use the latest stable 32bit version of Lazarus and FPC. At the time of this signature that is Laz 3.0RC2 and FPC 3.2.2
OS: Windows 10 64 bit

 

TinyPortal © 2005-2018