By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,333 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,333 IT Pros & Developers. It's quick & easy.

Thread Exception

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Homa" <ho******@yahoo.com> wrote in message
news:a9**************************@posting.google.c om...
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

Nov 13 '05 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3

P: n/a
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
"Homa Wong" <no*****@MyWorld.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.