• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions thrown in finally block  RSS feed

 
Heather Barnes
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can someone please explain this? The code compiles because m2(); is masked by m3(); in finally?


 
Pete Letkeman
Ranch Foreman
Posts: 916
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you include a finally in you try block then there are only a few times that it will not run. One of the times that it does not run is when you call System.exit(IntNumHere).

Line 8 calls m2 inside of a try block and throws an exception. However the fact the in the finally block from m2 contains a call to m3 and m3 throws an exception and which is not handled it is the exception that causes the program to halt.
What is occurring is nested exception throwing. The deepest exception which is not caught is the one that you are seeing the call stack when run.

I've added a second try/catch block inside of the first finally block so that you can see what is happening and when.


I don't know if there is a limit to the number of levels in a nested exceptions what Java can handle, however good programming would dictate that this level small so that it's easy to read/follow/maintain.

One other note: The finally block runs even if there is a return statement in the try/catch block preceding it.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that from a book or certification website? If so, please tell us where it comes from, to avoid copyright problems and so we can assess the source for ourselve.

No, it compiles because you are declaring the method as throwing any kind of Exception. Whether you call m2() or m3(), the exception thrown is included in throws Exception.
If you throw an Exception in a finally, remember that the finally always runs irrespective of whether the try completes normally or not, That means that any Exceptions thrown in the finally override anything thrown elsewhere, and any values returned from a finally override anything returned elsewhere.

I hope you don't indent code like that yourself.
 
Pete Letkeman
Ranch Foreman
Posts: 916
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie, I see what I did incorrectly with the formatting of the code. Sorry about that, I know that I missed lining up one set of brace brackets.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The indentation is far worse than that, I am afraid.
 
Heather Barnes
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a question from enthuware.
 
Pete Letkeman
Ranch Foreman
Posts: 916
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie,

Due to the fact I'm trying to learn for the OCA exam I am using Notepad, otherwise I would be using IntellJ.

I know that I do make many mistakes, all the time in fact and I am not trying to be rude or anything like that, however would it be okay if we circled back to the main point of the thread? I shall endeavor to write better code, which I hope will be formatted better, and learn more every Java day.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use NotePad2 or NotePad++ instead of NotePad. The other two are much much better. I use NotePad++ if I ever use Windows®.
It is not you who indented the code badly; you simply copied it. I hadn't even seen your post when I commented about indentation.
Maybe Enthuware indented it like that. Unfortunately exam preparation resources often show badly‑formatted code in the hope that the formatting will confuse the learner and cause them to miss important features.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!