"Hi, Amigo!

"I want to tell you about something small, but interesting."

"I'm listening. I love things that are small and interesting."

"Well, you know that every Thread object has a run() method. And that you can execute it on a separate thread using the start() method."

"Yes, of course."

"But now imagine this situation: you start a thread to perform some work, but an exception is thrown and the thread stops running because it doesn't know what to do. Wouldn't you need to know about this error somehow?"

"I agree. I'd need to somehow catch the exception that occurred on the other running thread. Does Java even support that?"

"You insult me. Of course it does."

"Java's creators invented a special interface called UncaughtExceptionHandler. Here's how to catch and handle an exception that occurs on another thread, if that thread does not catch it:"

Example
public class DownloadManager
{
 public static void main(String[] args)
 {
   Thread thread = new DownloadThread();
   thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
   {
    @Override
    public void uncaughtException(Thread t, Throwable e)
    {

    }
 });

 thread.start();
}

"The Thread object has a special setUncaughtExceptionHandler method. You need to pass it an object that implements the Thread.UncaughtExceptionHandler interface. This interface has only one method: uncaughtException(Thread t, Throwable e). This is the method that will be called on the passed object if an uncaught exception occurs in the run method."

"In my example above, I simply declare an anonymous inner class (highlighted in red) that implements the Thread.Thread.UncaughtExceptionHandler interface. And I override its uncaughtException(Thread t, Throwable e) method."

"As you can see from the method's parameter list, two arguments will be passed: a reference to the Thread object where the exception occurred, and the exception itself, passed as Throwable e."

"Well, why do I need the Thread variable t? Don't we already know which Thread we're putting a Thread.UncaughtExceptionHandler object in?"

"They did this so that you can write a universal handler for these situations. i.e. you can create a single object and pass it to dozens of different threads. Then the uncaughtException(Thread t, Throwable e) method always gives you a reference to the Thread object where the exception occurred."

"What's more, you can create dozens of threads, for example, in a loop to perform specific tasks. In general, this reference to the Thread object won't be superfluous. I promise you that."

"I believe you. You've never been wrong."