Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trap exception from background thread

concurrent colleagues.

I need to be able to trap an exception that might be thrown from a background thread.

Let the code speak for itself (it is a bad code)

 public delegate bool CheckForUpdatesHandler(Uri uri);
    public class UpdatesChecker {
        public event AsyncCompletedEventHandler CheckForUpdatesAsyncCompleted;
        protected virtual void OnCheckForUpdatesAsyncCompleted(AsyncCompletedEventArgs args) {
            if (CheckForUpdatesAsyncCompleted != null)
                CheckForUpdatesAsyncCompleted(this, args);
        }

        public bool CheckForUpdates(Uri ftp) {            
            Thread.Sleep(1000);
            throw new Exception("bla");
            return true;
        }     


        public void CheckForUpdatesAsync(Uri ftp){            
            var action = new CheckForUpdatesHandler(CheckForUpdates);
            var c=action.BeginInvoke(ftp, delegate(IAsyncResult state) {
                OnCheckForUpdatesAsyncCompleted(new AsyncCompletedEventArgs(null, false, null));
            }, null);
        }    
    }
like image 337
Valentin V Avatar asked Feb 10 '09 13:02

Valentin V


People also ask

How do you catch an exception in a thread?

Exception handling in Thread : By default run() method doesn't throw any exception, so all checked exceptions inside the run method has to be caught and handled there only and for runtime exceptions we can use UncaughtExceptionHandler.

How do you handle an unhandled exception in the thread?

Uncaught exception handler will be used to demonstrate the use of exception with thread. It is a specific interface provided by Java to handle exception in the thread run method. There are two methods to create a thread: Extend the thread Class (java.

Can exceptions be handled outside threads?

All uncaught exceptions are handled by code outside of the run() method before the thread terminates. The default exception handler is a Java method; it can be overridden. This means that it is possible for a program to write a new default exception handler.

How can you catch an exception thrown by another thread in Java?

There does not exist a way in Java to use try/catch around your start() method to catch the exceptions thrown from a secondary thread and remain multithreaded.


1 Answers

With Delegate.BeginInvoke, the exception will be retrieved by calling .EndInvoke - which you must do anyway to prevent a leak.

With BackgroundWorker, it will appear on the completion event

On a vanilla Thread, an unhandled exception will tear down the process.

However, the simplest approach is: don't let it throw...

    public bool CheckForUpdates(Uri ftp) {
        try {
            Thread.Sleep(1000);
            throw new Exception("bla");
            return true;
        } catch (Exception ex) {
            // raise an event, call a method/callback, do something
        }
    }

If you currently aren't using EndInvoke, then perhaps switch to the above pattern and just use ThreadPool directly (rather than Delegate.BeginInvoke).

like image 150
Marc Gravell Avatar answered Sep 28 '22 13:09

Marc Gravell