Forum > Operating Systems

How to catch an exception raised within a thread ?



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

--- Code: ---while not terminated do
sleep a while


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

synchronizing and logging error message
--- End code ---

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....

Most DB controls may not be thread-safe.

How about use critical section or similar method at thread?

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) ?

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:


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


[0] Message Index

Go to full version