Return in java exception handling

If execution does not raise an exception, then control passes to the final block. So is the return statement in the try block ignored by the JVM? , Or, if an exception occurs, then control passes to the catch block, and also ignores the returned status and control passes to the final block and returns from the final

public class Helper { public int showException(int a, int b){ try{ int c=a/b; return c; } catch(Exception e){ return 0; } finally{ return 3; } } } 
+5
source share
5 answers

Since the finally block will execute every time if you enter try or catch , I think its name is finally :)

FROM JAVA DOCS

The finally block is always executed when the try block completes. This ensures that the finally block is executed even if an unexpected exception is thrown.

Note: it will not be executed only when

If the JVM terminates while try or catch code is running, then the finally block may not be executed. Similarly, if the execution of a stream of try or catch code is interrupted or killed, the finally block may not be executed, even if the application as a whole continues.

+4
source

By design, returning to the finally block always takes precedence.

+2
source

Finally, it will be called last, whether the catch block is called or not. You might want to read additional documentation. MSDN Try Catch Finally

0
source

the finally block is always executed regardless of whether the catch try block is executed or not

0
source

The finally block is always executed when the try block completes. This ensures that the finally block is executed even if an unexpected exception is thrown. But, finally, processing is not only useful for exceptions - this allows the programmer to avoid the cleanup code accidentally bypassing with a return, continues or is interrupted. Putting clean code in the finally block is always good practice, even if no exceptions are expected.

More here

0
source

All Articles