Forum Stats

  • 3,852,270 Users
  • 2,264,086 Discussions
  • 7,905,017 Comments

Discussions

can we leave finally block without executing in exception handling ?

843789
843789 Member Posts: 46,655 Green Ribbon
I exception handling whenever we wrote some code inside finally block, that must be executed. Is that any idea we will make the choice to execute the finally block or can we leave the finally block without executing ? Whether it is possible ??
If somebody have idea about this please suggese....
or you can directly write me at [email protected]
Thanks in advance

Comments

  • 791266
    791266 Member Posts: 18,005
    The finally block will always execute unless the VM is terminated.
  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    in the cath block if we write "System.exit(0)" then finaaly block wont be executed.
    System.exit(0) means we r stopping the jvm explicitly.
  • 3004
    3004 Member Posts: 204,171 Green Ribbon
    The entire point of the finally block is that it always executes (unless the thread terminates first). If there's something that you only want to execute sometimes, then either a) don't put it in finally, or b) use an if or other control structure. It's impossible to say what the appropriate approach is without more details about what you're trying to accomplish and what your code currently looks like.
  • 3004
    3004 Member Posts: 204,171 Green Ribbon
    gsjames wrote:
    in the cath block if we write "System.exit(0)"
    then we're doing something wrong.

    There's almost never a reason to call System.exit() explicitly, and certainly not within a catch block.
This discussion has been closed.