• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we define try without finally

 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know we can define try without catch but can we define try without finally?

Kindly reply...
 
Veeresh Hosur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If catch block is there then finally block is optional. If catch block is not there then finally block is mandatory.

Thanks
Veeresh
 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is that means try should be followed by any one thing either catch or finally?Rectify me if i understood wrong.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"try" cannot exist on its own. It must be followed by one or more "catch" blocks and/or a "finally". You can, and often do, have both "catch" and "finally".
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A try-block should be followed by either a catch-block, a finally-block, or both.

Note: It's very easy to try this out yourself. Write a small test program with a try-block, and add or leave out a catch- or finally-block. Try to compile it and see what the compiler says.

Experimenting yourself is important when you learn Java. Trying it out for yourself is one of the best ways to learn things.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic