0

I managed to bypass the try catch block, by nesting multiple threads.

Is therere some rule, where it is documented, when the try catch block is bypassed by Exceptions?

try{
  Runnable r = new Runnable() {

    @Override
    public void run() {
      System.out.println("Thread");

      Display.getDefault().syncExec(new Runnable() {
        @Override
        public void run() {
          System.out.println("ThreadGUI");
          throw new NullPointerException();
        }
      });

    }
  };

  Thread t = new Thread(r);
  t.start();

} catch(NullPointerException e) {
  //nothing
}

System.out.println("Ende");
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
Skip
  • 6,240
  • 11
  • 67
  • 117
  • 1
    Just because thread A started thread B which threw an exception doesn't mean thread A will receive B's exceptions. – Louis Wasserman Nov 27 '12 at 17:31
  • 1
    Not really sure what you were expecting here. The exception happens in a different thread. – tmyklebu Nov 27 '12 at 17:32
  • 1
    Why do you want to run it in a different thread? – ShyJ Nov 27 '12 at 17:35
  • Because on one hand you use `syncExec` - which means you want to wait before the operation ends, and on second hand you create a new thread - which means you want to run it asynchronously. Why don't you just use [asyncExec](http://stackoverflow.com/questions/11100171/difference-between-syncexec-and-asyncexec-of-display-class)? – ShyJ Nov 27 '12 at 20:52
  • Off course it is an artificially constructed example. I used a syncExec, because otherwise my main thread terminated, before the second thread could do it's job. – Skip Nov 28 '12 at 08:14

2 Answers2

4

Exceptions don't automatically propagate across thread boundaries. If you throw an exception in a particular thread, you can only catch it in that thread. The lexical structure of your code makes no difference in this respect.

The following are the relevant parts of the JLS:

During the process of throwing an exception, the Java virtual machine abruptly completes, one by one, any expressions, statements, method and constructor invocations, initializers, and field initialization expressions that have begun but not completed execution in the current thread. This process continues until a handler is found that indicates that it handles that particular exception by naming the class of the exception or a superclass of the class of the exception (§11.2). If no such handler is found, then the exception may be handled by one of a hierarchy of uncaught exception handlers (§11.3) - thus every effort is made to avoid letting an exception go unhandled.

...

If no catch clause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Your exception is thrown out in a different thread. This is why it is not caught. You might want to catch it inside tyour new thread and somehow propagate it to the main one.

ShyJ
  • 4,560
  • 1
  • 19
  • 19