Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:05:39+00:00 2026-06-10T16:05:39+00:00

I have a thread that is very simple. The only thing the Thread object

  • 0

I have a thread that is very simple. The only thing the Thread object does is throw new Exception();. The reason why I did that is because I needed to test what happens if a exception happens in a separate thread in my application. I have tested this with a “real” run time exception and my problem is the same.

What I do before I call the thread is:

AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;

//To handle exceptions in another thread
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
    //Do nothing ignore exception
}

//To handle all exceptions in main thread
private static void CurrentOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs)
{
    dispatcherUnhandledExceptionEventArgs.Handled = true;
}

When this thread does throw a exception it first hits CurrentDomainOnUnhandledException like it should, and then it does not hit CurrentOnDispatcherUnhandledException. Right after CurrentDomainOnUnhandledException The stack then moves back to the Thread where it throws the exception and throws it again, executing the same line. It never stops doing this. It is basically a never ending loop and I can not figure out why.

The UnhandledExceptionEventArgs does not have a property to set, to say we have handled the exception and I am pretty sure this is part of the problem.

What am I doing wrong, how to I catch this UnhandledException in this thread, and let the thread continue to execute (preferred) or abort / kill the thread (not preferred) so that it does not continue to execute the throw new exception line.

I have tested this with debug and release (made sure debugging info on release was set to none) and this still happens in both versions, the exception continues to happen over and over again.

Thanks in advance, I am just stumped and have no idea why it does it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:05:40+00:00Added an answer on June 10, 2026 at 4:05 pm

    When this thread does throw a exception it first hits CurrentDomainOnUnhandledException like it should, and then it does not hit CurrentOnDispatcherUnhandledException. Right after CurrentDomainOnUnhandledException The stack then moves back to the Thread where it throws the exception and throws it again, executing the same line. It never stops doing this. It is basically a never ending loop and I can not figure out why.

    You never handled the exception. You just returned back to the very same code that faulted in the first place. Say the code does this:

    i = 1 / 0;
    foo(i);
    

    And say you catch the division by zero error, but in the handler, you do nothing but return. Well, what would you expect to happen? The code encountered a fatal error that makes further progress impossible and you did nothing to fix it. What value should i contain?!

    Essentially, don’t try to catch exceptions except as part of the design of the code that throws them. It just doesn’t make sense, and whatever your outer problem is, there’s probably a sensible way to do it.

    But if a thread fails horribly, its process fails horribly. That’s the nature of threads — there’s no isolation. Consider:

    1. Acquire lock
    2. Modify some object
    3. Release lock

    What happens if the exception is thrown halfway through step 2? If your exception handler don’t release the lock, the next thread to try to acquire it waits forever. If your exception handler does release the lock, the next thread to acquire it will access an object that’s partially modified and perhaps in an inconsistent state. So an exception handler would have to make an intelligent decision, understand the code that threw the exception, as to what to do prior to releasing any locks held when the exception was thrown.

    If you’re going to catch the exception, you must figure out what the underlying problem is and repair it. In general, you can only do this if you fully understand the code that threw the exception. There’s no generic way to do it because there’s no generic way to repair a potentially corrupt object.

    Ok, then how do I kill the thread in CurrentDomainOnUnhandledException so it stops looping the exception?

    Generally that’s not what you want to do. What if that thread holds locks? What if that thread allocated memory that it needs to release?

    If you need this kind of isolation, use processes, not threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a very simple table with only one row that has a uiswitch
I have a thread that does the following: 1) Do some work 2) Wait
I have a very simple update statement: UPDATE W SET state='thing' WHERE state NOT
I have some very simple code to do with handlers: Handler seconds=new Handler() {
I have a very simple queue implementation that wraps a fixed array. It contains
A very simple approach. Two threads. volatile __int32 p=0; One thread (A) only use
I have one thread that is receiving data over a socket like this: while
I have a thread that downloads some images from internet using different proxies. Sometimes
I have a thread that sends GPS coordinates to a database every six seconds
* So I have a Thread that holds a sleep method and I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.