简体   繁体   中英

How can i catch another threads exception

Code:

public void doSomethingOrThrowUncheckedException()
{

    Thread worker = new Thread(new Runnable() {
            public void run() {
                try {
                    myObject.doSomething()
                } catch(CheckedException e) {
                    new UncheckedException ();
                }
            }
        });
    worker.start();
}

Explanation

I want to perform some work in another thread that can throw a checked exception.

I cannot wait for this work to finish and I want the method caller to know if something went wrong with this work since the new thread is not able to handle the exception properly.

Is there a mechanism I can use?

Can you create a Observable outside of the thread? If something goes wrong, the thread sets a value to change that Observable. The main code is an Observer and reacts to the change when the property listener is called.

It depends on what you mean by the caller knowing that something went wrong. A couple of options come to mind immediately.

  • The worker thread can set an error flag. The disadvantage is that the calling thread will need to check the flag to know that something went wrong. (There can also be a flag for success; as long as neither is set, the calling thread knows that the worker is still working.

  • The worker thread can call an error method. The disadvantage is that the call will take place in the worker thread. On the other hand, this provides a place to take positive action. The Observer pattern might be useful here (although I think Java's implementation is terrible).

If, when the worker thread completes successfully it communicates the success to Foo, or produces an object that Foo consumes, then expand that mechanism to allow it to pass the checked exception along to Foo, rather than passing the exception to the method calling thread.

public void doSomething()
{
    Thread worker = new Thread(new Runnable() {
            public void run() {
                try {
                    result = myObject.doSomething();
                    foo.processResult(result);
                } catch(CheckedException e) {
                    foo.processException(e);
                }
            }
        });
    worker.start();
}

public void doSomething()
{

    Thread worker = new Thread(new Runnable() {
            public void run() {
                try {
                    result = myObject.doSomething();
                    resultQueue.add(result);
                } catch(CheckedException e) {
                    resultQueue.add(e);
                }
            }
        });
    worker.start();
}

If doSomething() doesn't interact with anything when it successfully completes then you'll need to follow one of the other answers.

您可能还想看看uncaughtExceptionhandler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM