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

0 votes
Can  I get the explanation with the help of code
Dec 3, 2018 in Java by Neha
• 6,300 points
20,166 views
Young Heaven - Naked Teens & Young Porn Pictures
http://baitbus.fetlifeblog.com/?madilyn

 vanessa b porn star porn stars with huge boobs porn addiction meth machine teen porn free porn or sex stories
Best Nude Playmates & Centerfolds, Beautiful galleries daily updates
http://xxx.porn.allproblog.com/?anissa

 monica beach porn sexy athletic coed porn videos reality tv stars does porn videos penis sword fighting porn alabama porn tubes
Girls of Desire: All babes in one place, crazy, art
http://mexican.porn.bloglag.com/?jaylynn

 brittany spears flash video porn free streaming porn beta channels mature women sex porn free yong fresh pussy poron blondes porn pictures of cartoon sex porn
Nude Sex Pics, Sexy Naked Women, Hot Girls Porn
http://pornbigfatgirl.instakink.com/?marie

 lori anderson porn tubes classic porn trails free young cheerleading porn old lady porn free videos how to remove porn from computer
College Girls Porn Pics
http://free.lesbian.bestsexyblog.com/?adrianna

 ma x porn soprano porn star fievres nocturnes porn college physicals porn good free online porn yahoo answers
Hot teen pics
http://hdlesbians.relayblog.com/?armani

 video de anime porn tran porn russian porn liudmila most popular free porn websites brazilina porn
Dirty Porn Photos, daily updated galleries
http://amateurthemovie.ebonyellisemory.hoterika.com/?jaden

 young interratial porn tabelle porn porn cousin milkers porn star andi anderson bio remove porn from my computer

1 answer to this question.

0 votes

This can be done using Thread.UncaughtExceptionHandler.

Here’s a simple example:

// create our uncaught exception handler
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread th, Throwable ex) {
        System.out.println("Uncaught exception: " + ex);
    }
};

// create another thread
Thread otherThread = new Thread() {
    public void run() {
        System.out.println("Sleeping ...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("Interrupted.");
        }
        System.out.println("Throwing exception ...");
        throw new RuntimeException();
    }
};

// set our uncaught exception handler as the one to be used when the new thread
// throws an uncaught exception
otherThread.setUncaughtExceptionHandler(handler);

// start the other thread - our uncaught exception handler will be invoked when
// the other thread throws an uncaught exception
otherThread.start();
answered Dec 3, 2018 by Frankie
• 9,830 points

Related Questions In Java

0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
678 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,910 points
11,656 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,370 points
694 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 26, 2018 in Java by developer_1
• 3,320 points
890 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered May 1, 2018 in Java by sharth
• 3,370 points
915 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
781 views
0 votes
1 answer

How to retrieve current stack trace?

Yes, in Java we do have a ...READ MORE

answered May 17, 2018 in Java by code.reaper12
• 3,500 points
1,614 views
0 votes
1 answer

What do you mean by aggregation in Java?

Aggregation is a specialized form of Association ...READ MORE

answered Feb 18, 2019 in Java by Frankie
• 9,830 points
536 views
0 votes
1 answer

How to create a temporary directory/folder in java?

If you are using JDK 7 use ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,830 points
6,105 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP