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

  • Home
  • SEARCH
  • 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 7923805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:26:02+00:00 2026-06-03T17:26:02+00:00

If an Exception occurs at some point in Java code, the control goes to

  • 0

If an Exception occurs at some point in Java code, the control goes to the catch block. However, If this code is actually inside a multi-threaded run() method, how do I instruct the code the to finish with the run() method when the Exception occurs?

In other words, how can I instruct the thread that the run() method has finished, because it knows that an Exception has occurred?

  • 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-03T17:26:05+00:00Added an answer on June 3, 2026 at 5:26 pm

    If you’re in the run() method of your Thread, you can simply return, which terminates the Thread in the same way that calling return in any other method ends the method processing.

    Maybe something like this, where you watch for an Exception and call return if the Exception occurs…

    public void run() {
        try {
            // your code goes here
        }
        catch (Exception e) {
            return;
        }
    }
    

    However, if your Thread has multiple methods, the best way might be to have each method declare that it can throw an Exception. For example, if your Thread is like this…

    public MyThread extends Thread {
    
        public void run() {
            try {
                runLongTask1();
                runLongTask2();
            }
            catch (Exception e) {
                return;
            }
        }
    
        public void runLongTask1() throws Exception {
            // your code here
        }
    
        public void runLongTask2() throws Exception {
            // your code here
        }
    }
    

    See how each of the other methods can throw an Exception. So, if an Exception is thrown in any of the methods of this Thread, it will trigger the try-catch of the run() method, and return out of the Thread, thus terminating it.

    Of course, if the try-catch in your run() method covers all the code of the method, you don’t need to actually return, because it is implicit when it reaches the end of the run() method anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that this part of my code is where the exception occurs: c
Can anyone point out the flaw in this code? I'm retrieving some HTML with
This is an intermittent exception that occurs intermittently in Umbraco 3.0.4 running in IIS
Exception in thread Thread-2 java.lang.NumberFormatException: For input string: 3 int test = Integer.parseInt(result[0]); This
I have implemented a java program . This is basically a multi threaded service
When an exception occurs in my QUnit tests, all it will say is Died
While I perform insertion in the db table, the exception occurs, which says: Data
Exception in thread Thread-0 java.lang.NullPointerException at org.apache.http.impl.nio.reactor.AbstractIOReactor.closeActiveChannels(AbstractIOReactor.java:532) at org.apache.http.impl.nio.reactor.AbstractIOReactor.hardShutdown(AbstractIOReactor.java:564) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.doShutdown(AbstractMultiworkerIOReactor.java:411) at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:340) at
Argument exception error is thrown at the following code. It's a simple code but
In the following code, am executing a java program Newsworthy_CA. The program prints the

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.