Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:48:19+00:00 2026-06-13T00:48:19+00:00

I have a Controller class and a Monitor worker thread. The controller thread looks

  • 0

I have a Controller class and a Monitor worker thread.
The controller thread looks something like this

public class ControllerA {
    public void ControllerA(){
        try{
            doWork();
        }
        catch(OhNoException e){
        //catch exception
        }

    public void doWork() throws OhNoException{

      new Thread(new Runnable(){
        public void run(){
        //Needs to monitor resources of ControllerA, 
        //if things go wrong, it needs to throw OhNoException for its parent
        }
        }).start();

      //do work here

    }
}

Is such setup feasible? How do I throw exception to the outside of the thread?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:48:20+00:00Added an answer on June 13, 2026 at 12:48 am

    How do I throw exception to the outside of the thread?

    Couple ways you can do this. You can set a UncaughtExceptionHandler on the thread or you can use an ExecutorService.submit(Callable) and use the exception that you get from the Future.get().

    The easiest way is to use the ExecutorService:

    ExecutorService threadPool = Executors.newSingleThreadScheduledExecutor();
    Future<Void> future = threadPool.submit(new Callable<Void>() {
          public Void call() throws Exception {
             // can throw OhNoException here
             return null;
         }
    });
    // you need to shut down the pool after submitting the last task
    threadPool.shutdown();
    try {
       // this waits for your background task to finish, it throws if the task threw
       future.get();
    } catch (ExecutionException e) {
        // this is the exception thrown by the call() which could be a OhNoException
        Throwable cause = e.getCause();
         if (cause instanceof OhNoException) {
            throw (OhNoException)cause;
         } else if (cause instanceof RuntimeException) {
            throw (RuntimeException)cause;
         }
    }
    

    If you want to use the UncaughtExceptionHandler then you can do something like:

     Thread thread = new Thread(...);
     final AtomicReference throwableReference = new AtomicReference<Throwable>();
     thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
         public void uncaughtException(Thread t, Throwable e) {
             throwableReference.set(e);
         }
     });
     thread.start();
     thread.join();
     Throwable throwable = throwableReference.get();
     if (throwable != null) {
         if (throwable instanceof OhNoException) {
            throw (OhNoException)throwable;
         } else if (throwable instanceof RuntimeException) {
            throw (RuntimeException)throwable;
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller that looks like this: public class PageController : Controller {
Lets say I have a controller class like this. public class InStorePickupController : Controller
I have this controller: public class StandingsController : Controller { public ViewResult Index(int id)
Using Spring MVC, I have this Controller class: @Controller public class LoginController { @Autowired
Have Controller: public class MyController : Controller { [HttpGet] public ActionResult MyAction(int iMode, string
I have a controller class as in the following code segment: @Controller public class
Given I have a controller class as such: public class ResourceController : AuthorizedController {
I have the following controller: class Tests extends CI_Controller { public function update_record_test() {
I have a MY_Controller class with this property: class MY_Controller extends CI_Controller { public
I have @Controller @RequestMapping(value=core/*) public class CoreController { public static String exceptionOccurredView = /core/exceptionOccurred;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.