0

I am creating a new Thread from the main thread using the below:

public static void main(String[] args) {

        try {
            new TestThread().start();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Inside main");

    }

And throwing an exception. I am able to catch it in the main thread also.But according to article

this shouldn't be the case right?

EDIT:

public class TestThread extends Thread {

@Override
public void run() {

throw new RuntimeException();
}

 }

Exception Trace

Inside main
Exception in thread "Thread-0" java.lang.RuntimeException
    at TestThread.run(TestThread.java:8)
ghostrider
  • 2,046
  • 3
  • 23
  • 46
  • What exception are we talking about? – Amongalen Jan 24 '20 at 12:36
  • 1
    Please provide a [mcve] – Lino Jan 24 '20 at 12:36
  • 1
    What makes you think that you are catching it in the main thread? – Thilo Jan 24 '20 at 13:32
  • The main thread can only catch exceptions that occur inside of the constructor or inside of `start` (for example not enough free memory to create another thread). You don't get to see exceptions from `run`. – Thilo Jan 24 '20 at 13:38
  • 1
    Re, "...child thread..." The knowledge that thread B was created by thread A might mean something to _you_, but the JVM does not care. There's several reasons why it does not make sense for an uncaught exception in a thread to be re-thrown in the "parent" thread, but in Java, one of those reasons is that the JVM won't even remember which thread is the "parent." – Solomon Slow Jan 24 '20 at 13:53

2 Answers2

2

Exceptions thrown by a thread's run() method, are not caught by the main thread but handled by the JVM.
If you want to manage this situation, you should use the Thread.setUncaughtExceptionHandler method.

Something like this:

class MyHandler implements Thread.UncaughtExceptionHandler
{
  @Override
  public void uncaughtException(Thread t, Throwable e)
  {
    System.out.println("Caught exception " + e.getMessage());
    e.printStackTrace();
  }
}

class MyThread extends Thread
{
  public MyThread()
  {
    setUncaughtExceptionHandler(new MyHandler());
  }

  @Override
  public void run()
  {
    throw new RuntimeException("ciao");
  }
}
Robert Kock
  • 5,795
  • 1
  • 12
  • 20
  • But I am catching the exception in the main as per the code. – ghostrider Jan 24 '20 at 13:52
  • 3
    @ghostrider No you're not. Try changing the exception handler in your main thread. You'll see it doesn't get there. The output you see now comes directly from the JVM – Robert Kock Jan 24 '20 at 14:01
  • 2
    @ghostrider, Re, "I am catching the exception in main..." You wrote an exception handler that _would_ catch an exception thrown by `start()` _IF_ `start()` threw an exception. But the exception that is thrown in the new thread will not cause `start()` to thrown an exception. In fact, it's possible that `start()` could return, and the main thread could print its message and _terminate_ before the new thread gets to throw its exception. – Solomon Slow Jan 24 '20 at 14:28
0

The article you refer to is "Java Thread: Run method cannot throw checked exception".

In Java, Thread's run method cannot throw checked exceptions because it would involve changing the signature of the run() method. However, in your example, you throw a RuntimeException, which isn't a checked exception. See Understanding checked vs unchecked exceptions in Java.

Note, however, that you are not actually catching the exception in the main thread: replace the printStackStrace statement with something like ...println("caught") for this to be more obvious.

SDJ
  • 4,083
  • 1
  • 17
  • 35
  • But that exception will still not propagate to the main thread somehow. All it will do is crash whatever thread is executing `run()`. – Thilo Jan 24 '20 at 13:34
  • @SDJ, I updated the question with the stacktrace. I think what you mean is I wont get the entry of .start() method which is present in the main thread right? – ghostrider Jan 24 '20 at 13:46