How to handle exceptions in other threads

Let’s say I have a custom thread object,

public abstract class CustomThread extends Thread {
// ....

@Override
pubic void run(){
     // ...
     try{
             methodThatThrowsAnException ();
     catch (Throwable e){ handleException(e);
}

// ....

public abstract void handleException(Throwable e);

Would it be bad practice to have an interface for catching & handling exceptions back in the main thread?

public static void main(String[] args){
     Thread secondaryThread = new AnotherCustomThread(new IForeignCatcher(){
            catchThrowable(Thread t, Throwable e){ e.printStackTrace(); }
     } ); 
     
     secondaryThread.start();
     // ...
}

I’m still new to threads, someone let me know if I’m doing this completely wrong xD

What would be the purpose of passing the exceptions back to the main thread?

The main thread is handling all my logging, and all the exceptions that could possibly happen. So it’s easier to pass the exceptions over there rather than making some static thing.

If you want multiple threads submitting to a logger, then do that: have a dedicated logging thread (probably a daemon), which reads from a blocking queue in a loop, and have whatever other threads push events onto the queue. Use it like a CSP channel from Go and others.

The threads in the background will be preforming generic tasks. I’m modelling it over something Elusivehawk make a long time ago. There’s a generic task object that does some job, that gets inserted into a pre-made runnable and started with a thread as a daemon. Any issues that could happen with processing the task is handled by the logger, which gets it’s exceptions from a interface-callback-thingy in the runnable. So the threads will only be running for a small amount of time.

But I do need some long term solution, because I will be having main threads for rendering, doing physics and such. I haven’t figured that out yet, but I think the exceptions will be handled the same way.

[quote]There’s a generic task object that does some job, that gets inserted into a pre-made runnable and started with a thread as a daemon. Any issues that could happen with processing the task is handled by the logger, which gets it’s exceptions from a interface-callback-thingy in the runnable.
[/quote]
Sounds like a good place for this: http://stackoverflow.com/questions/1687977/how-to-properly-catch-runtimeexceptions-from-executors


static ArrayList<Exception> exeptions;

	...catch (Exception e){
			synchronized(exeptions){
				exeptions.add(e);
			}
	}

=)