When will I see a ThreadEndException?

I’d like a little clarification about ThreadEndException.

I have a system that can create multiple Threads that are ultimately tied to a SSLSocket. When the user disconnects, all those threads need to be killed no matter what they are doing. As such, I created a ThreadKiller thread whose only job is to kill another Thread. That works perfectly and keeps the main thread from being blocked.

But since want to know what happened in a Thread, I added a bunch of logging in the superclass, something like:

Event Run
  Try
    Log "Thread Started"
    RaiseEvent Run
    Log "Thread Finished"

  Catch err as RuntimeException
    if err isa EndException or err isa ThreadEndException then
      Log "Thread told to end"
      raise err
    end if

    Log DataAboutException( err )
  End Try

(This is pseudo-code to give you an idea.)

When the Thread runs its course, I see “Thread Finished”. When there is an error in the Thread code, I get information about the exception. But when I kill the Thread, I don’t get anything. I know the Thread died, but it goes away silently. Why?

I would expect ThreadEndException is not to be catched by you.

I think I have seen it which is why I started coding defensively against it and EndException. The LR even warns that you should re-raise it when you encounter it.

NOTE: Whenever you inadvertently catch an ThreadEndException, you MUST re-raise it. Failing to do so will mess up the runtime environment and lead to unpredictable problems. See Exception statement for more information about its proper use.

http://documentation.xojo.com/index.php/ThreadEndException

I’d still like an answer but solved the immediate problem by adding a Destructor. If IsFinished is still false, it means the Thread never got a chance to clean up so I can handle it there.

ThreadEndException should only show up when quitting. Are you doing this?

I don’t see it there either.

It’s been quite a while since I saw that exception.

All my methods and events end with

exception exc theException = new ErrorException(exc, currentMethodName)

where ErrorException is a class where the constructor starts with

[code]Sub Constructor(theError as RuntimeException, Location as string)

if theError isA EndException or theError isA ThreadEndException then
  'nothing to do
  Raise theError
else
   'do something with theError

end if
End Sub[/code]

As far as I remember I had to add the ThreadEndException for exceptions in a thread.

I believe it can show up if you explicitly use Thread.Kill or if Quit is called (each living thread gets killed at that point).