Does a finally block always get executed in java exception handling?

linray

Fresh Meat
1
0
3
I have the site name JAVA EXCEPTION HANDLING,

I am learning Exception handling in java. I just want to know, does the finally() block always get executed in Java Exception Handling?

this code in java, can I be absolutely sure that the finally block always executes, no matter what something() is?
 

BRiT

CRaZY
Founder
12,514
8,056
637
I have the site name JAVA EXCEPTION HANDLING,

I am learning Exception handling in java. I just want to know, does the finally() block always get executed in Java Exception Handling?

this code in java, can I be absolutely sure that the finally block always executes, no matter what something() is?

In typical situations it will execute, but there are special circumstances wherein it won't. Such as out of memory or thread aborts or process shutdown or maybe stack overflows.
 

BlastemSkyHigh

The Original Fuck You Bomb
559
453
103
Yes, according to google,
the finally block is always get executed unless there is an abnormal program termination either resulting from a JVM crash or from a call to System. exit().
A finally block is always get executed whether the exception has occurred or not.
 

CoprophagousCop

¯\_(ツ)_/¯
Premium
2,883
2,893
357
On a side note, Java-style exception handling (try ... catch instructions) was created in response to sloppy, lazy programmers ignoring return values from functions. There are two type of run-time errors that cause exceptions: preventable errors and non-preventable errors. Preventable errors are a result of coding bugs, whereas non-preventable errors include things like memory allocation failures and missing files. Unfortunately, with exception handling, both types of errors get handled in the same way. If people wrote more robust code to check for the non-preventable errors, such as checking for null pointers when memory is allocated or invalid handles when files are opened, exception handling would not really be needed.