Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

why finally block executes first in this code below ?  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why do I get output as Finished!Joyce Hopper  . My question is why didn't try block execute first ?
 
Ranch Hand
Posts: 246
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:why do I get output as Finished!Joyce Hopper  . My question is why didn't try block execute first ?



The try block did execute first.  Finally always runs (except for System.exit/JVM shutdown/etc.).  In this case, it prints before the return (clause) actually returns to its caller.

You could add System.out.println("before return"); before the return statement in your try to get a better visual on this.  
 
Marshal
Posts: 63489
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you get that code to compile with finally before catch?
 
Charles O'Leary
Ranch Hand
Posts: 246
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How did you get that code to compile with finally before catch?

Great point Campbell.  
 
Charles O'Leary
Ranch Hand
Posts: 246
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:

Md Zuanyeed Kamal wrote:why do I get output as Finished!Joyce Hopper  . My question is why didn't try block execute first ?



The try block did execute first.  Finally always runs (except for System.exit/JVM shutdown/etc.).  In this case, it prints before the return (clause) actually returns to its caller.

You could add System.out.println("before return"); before the return statement in your try to get a better visual on this.  



To Campbell's point (and my earlier point):



output:
inside try and before return
Finished!Joyce Hopper
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:

Campbell Ritchie wrote:How did you get that code to compile with finally before catch?

Great point Campbell.  



Sorry,  putting finally block before catch block was a mistake . The correct version is :  




The code has been adapted from JAVA OCA/OCP test prep guide.
 
Campbell Ritchie
Marshal
Posts: 63489
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:. . . The code has been adapted from JAVA OCA/OCP test prep guide.

Please tell us which guide, page number, etc., and also it is useful to see the real code without changes. Did the original really call toString() on a String?

Since it is a cert exam guide, the book might not tell you why catching a null pointer exception like that might be a bad idea.

The reason you are getting “Finished!” printed first is that line 14 cannot print anything until after the getFullName() method completes. “Finished!” is part of the getFullName() method, so it is printed before the return value.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Md Zuanyeed Kamal wrote:. . . The code has been adapted from JAVA OCA/OCP test prep guide.

Please tell us which guide, page number, etc., and also it is useful to see the real code without changes. Did the original really call toString() on a String?



OCA/OCP
Java® SE 8 Programmer
Practice Tests
by Scott Selikoff
Jeanne Boyarsky

Chapter 8 problem 28
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:

Campbell Ritchie wrote:

Md Zuanyeed Kamal wrote:. . . The code has been adapted from JAVA OCA/OCP test prep guide.

Please tell us which guide, page number, etc., and also it is useful to see the real code without changes. Did the original really call toString() on a String?



they called toString() .

 
Campbell Ritchie
Marshal
Posts: 63489
207
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote: . . . they called toString() .

I have never seen that done before, calling toString() on a String, but it is possible. Like all objects, Strings have a toString() method, which looks rather like this:-Maybe they were hoping to confuse you with the unusual call. Remember that cert exam practice questions often have confusing features.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!