CodeGym /Java Course /Java Multithreading /Practice catching thread errors

Practice catching thread errors

Java Multithreading
Level 5 , Lesson 9
Available

"How come you always get exactly 3 tasks?"

"How about doing another 50?"

"50? O_o"

"I guess it would be better to just do the usual 3."

14
Task
Java Multithreading, level 5, lesson 9
Locked
Wait and see
All exceptions that occur while the Solution thread is running must be handled by one of these options: 1. If it's an Error, then display "Can't keep running". 2. If it's an Exception, then display "Needs handling". 3. If it's a Throwable, then display "Wait and see". Implement this logic.
14
Task
Java Multithreading, level 5, lesson 9
Locked
Arming ourselves to the teeth!
Create your own UncaughtExceptionHandler as a local class inside the constructor. The UncaughtExceptionHandler should hide the thread name with asterisks and display a description of the error that occurred. "Thread-0" must be replaced with "********". "Thread-4321" must be replaced with "*********
14
Task
Java Multithreading, level 5, lesson 9
Locked
Charting our own course
Implement the UncaughtExceptionHandler interface in the Solution class, which must: 1. Interrupt the thread that threw the exception. 2. Display the exception's stack trace, starting with the innermost exception.
Comments (10)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
26 July 2021
50!! I will be died.
Andrei Level 41
28 April 2021
I barely understood a thing from these exercises... way too little information presented before about the problems at hand.
catalin1989 Level 37, Romania
25 January 2024
I barely understand the hole chapter :)). Hope Java Collections chapter is easier.
Andrei Level 41
30 January 2024
You need time, just keep going.
Ewerton Level 30, Belo Horizonte, Brasil
12 July 2019
On "Charting our own course", do not use recursion. It will not pass, just use iteration.
Senned Level 41, Azov, Russia
25 October 2019
I used recursion of uncaughtException metod and it passed.
BlueJavaBanana Level 37
9 September 2020
Recursion did indeed work for me too. Took me about four hours to figure out wtf code gym even wanted of course and then research all the stuff they havn't told/taught us. Anyone want guidance on this task, pm me.
Fadi Alsaidi Level 34, Carrollton, TX, USA
16 March 2021
I passed the assignment but have no clue how the iteration worked!!! the iteration is adding the throwable String representation to the list e.toString(); then assigning e to e = e,getCause(); I can't even for the life of me get more than one throwable created in main to debug what is going on. Please ping me or post some article here to help me undertsand. Thanks
Fadi Alsaidi Level 34, Carrollton, TX, USA
16 March 2021
replaying to my own replay to help others. read about chain Exceptions https://www.geeksforgeeks.org/chained-exceptions-java/
Dan Level 26, Clarksville, United States Expert
22 February 2022
For "Charting our own course": For anyone who, like Fadi, was having problems getting more than one exception thrown so that they could debug the issue: I copied this code from Bill Wu, who posted asking for help on this problem

    public static void main(String[] args) throws Exception {
        Solution s = new Solution();
        Thread.setDefaultUncaughtExceptionHandler(s);
        throw new Exception("ABC", new RuntimeException("DEF", new IllegalAccessException("GHI")));
    }
By using Thread.setDefaultUncaughtExceptionHandler(s), he sets the main thread (and all threads that don't have one set) to also use the exception handler we're creating. Then you can set public static void main(String[] args) to allow exceptions with the "throws Exception" tag. Once that's done you can just go ahead and throw the exception that CodeGym gives as an example.