Thread Exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Homa

    #1

    Thread Exception

    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 (ThreadAbortExc eption)
    {
    // It terminates
    return;
    }
    catch (OleDbException e)
    {
    // Table not found.
    if (e.Message.Star tsWith("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.Thr eadException and
    CurrentDomain.U nhandledExcepti on but neither works

    Best Regards,
    Homa
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Thread Exception

    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 ThreadAbortExce ption, 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]
    - nicholas.paldin o@exisconsultin g.com

    "Homa" <homaneag@yahoo .com> wrote in message
    news:a9a6fc0b.0 307141232.2a497 91d@posting.goo gle.com...[color=blue]
    > 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 (ThreadAbortExc eption)
    > {
    > // It terminates
    > return;
    > }
    > catch (OleDbException e)
    > {
    > // Table not found.
    > if (e.Message.Star tsWith("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.Thr eadException and
    > CurrentDomain.U nhandledExcepti on but neither works
    >
    > Best Regards,
    > Homa[/color]


    Comment

    • Homa Wong

      #3
      Re: Thread Exception

      Thank you for the advises. But how to solve the problem I mentioned? How
      do I catch the exception thrown by this thread (loadTableThrea d) 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!

      Comment

      • Mark Pearce

        #4
        Re: Thread Exception

        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 "Comprehens ive VB .NET Debugging"



        "Homa Wong" <notReal@MyWorl d.com> wrote in message
        news:udtZ24uSDH A.2676@TK2MSFTN GP10.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 (loadTableThrea d) 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!


        Comment

        Working...