Thread Exception

H

Homa

Hi,
I have a problem about thread related exception handling.
I created a background thread to fill a DataTable in a Windows
Application. Inside the code, I have
foreach (…)
{
try
{
da.Fill(dt);
}
catch (ThreadAbortException)
{
// It terminates
return;
}
catch (OleDbException e)
{
// Table not found.
if (e.Message.StartsWith("The Microsoft Jet database
engine cannot find the input table or query"))
break;
else
throw;
}
finally
{
conn.Close(); // Play safe
}
}
The problem is there are three kinds of OleDbException that
could be throw:
-- Table not Found, which I've taken care of (and this
consider normal to the program because it will search through
different tables
-- Connection error: The connection doesn't exist from the
beginning.
-- Network error: The connection was there but got
disconnected.

For the Connection error and Network error, I want the main
thread to trap it, display a message and continue to run.
How do I do it?
I tried Application.ThreadException and
CurrentDomain.UnhandledException but neither works

Best Regards,
Homa
 
N

Nicholas Paldino [.NET/C# MVP]

Homa,

I think that checking against the text of the message is a bad idea.
You should check the Errors property or the ErrorCode property to see what
the specific error is.

Also, when catching the ThreadAbortException, this is a special-case
exception that is automatically re-thrown. The only way to prevent the
thread from aborting is to call the static ResetAbort method on the Thread
class so that the exception doesn't propogate up the stack.

Hope this helps.
 
H

Homa Wong

Thank you for the advises. But how to solve the problem I mentioned? How
do I catch the exception thrown by this thread (loadTableThread) in the
main thread?

Did I made any mistake by saying
catch (OleDbException e)
{
if (e.errorCode = ...)
//handle the exception
else
throw e; // re-throw it
}


Thanks for concern,
Homa Wong
 
M

Mark Pearce

Hi Homa,

In your delegate that launches the background thread, you can supply a
callback method that will be called when your thread has finished running.
In that callback method, you can reconstruct the original thread delegate
and then call its EndInvoke method within a Try...Catch block. If your
background thread threw an exception that it didn't handle, calling
EndInvoke will automatically rethrow that exception.

I've got some demonstration code for this lying around somewhere if you want
it. Alternatively, if you read VB, you can download the free online chapter
at the link below that demonstrates this mechanism.

HTH,

Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128


Thank you for the advises. But how to solve the problem I mentioned? How
do I catch the exception thrown by this thread (loadTableThread) in the
main thread?

Did I made any mistake by saying
catch (OleDbException e)
{
if (e.errorCode = ...)
//handle the exception
else
throw e; // re-throw it
}


Thanks for concern,
Homa Wong
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top