+ 4

Garbage Collection in java

Why in the above code there is an error "error: unreported exception Throwable; must be caught or declared to be thrown t.finalize(); " If we remove throws Throwable then the required output will be achieved but why? https://code.sololearn.com/c69zyMWBuR13/?ref=app

30th Jun 2018, 1:10 AM
harshit
harshit - avatar
10 Respostas
+ 6
finalize claims to throw a Throwable exception. Therefore, show must catch it or also claim to throw it. If show passes it on, main must also do something about it.
30th Jun 2018, 1:54 AM
John Wells
John Wells - avatar
+ 5
I've updated code again. Rerun it.
30th Jun 2018, 3:43 AM
John Wells
John Wells - avatar
+ 3
If I add throws to main and show, I only get one t as output same as catching. Try running now.
30th Jun 2018, 3:32 AM
John Wells
John Wells - avatar
+ 3
Putting the try catch code puts a filter on exceptions. Each type of exception that has a catch block is processed there and is gone. Those that don't continue being passed up the call chain.
30th Jun 2018, 3:35 AM
John Wells
John Wells - avatar
+ 3
All exceptions are subclasses of Throwable so that catch stops them all.
30th Jun 2018, 3:38 AM
John Wells
John Wells - avatar
30th Jun 2018, 2:47 AM
John Wells
John Wells - avatar
+ 1
I understand how show and main can throw the same exception but can you please rewrite the code on how they can catch it?
30th Jun 2018, 2:42 AM
harshit
harshit - avatar
0
But in this code the output is only t but if we made show and main to "throws Throwable" then we will get: t t
30th Jun 2018, 3:15 AM
harshit
harshit - avatar
0
How does catch(Throwable error) work?
30th Jun 2018, 3:15 AM
harshit
harshit - avatar
0
oh,sorry. But how does the earlier code which has catch(Throwable error) works. Sorry,I am a beginner.
30th Jun 2018, 3:35 AM
harshit
harshit - avatar