filmov
tv
Java Interview Questions: Return in try catch finally block:

Показать описание
java interview questions: Try Catch Finally block
If you return the value from all three blocks {try, catch, and finally} which will be considered as the actual return value of the method?
Try: The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block.
Catch: You associate exception handlers with a try block by providing one or more catch blocks directly after the try block. No code can be between the end of the try block and the beginning of the first catch block.
Finally: The Finally block always executes when the try block exits. This ensures that the Finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a Finally block is always a good practice, even when no exceptions are anticipated.
If you return the value from all three blocks {try, catch, and finally} which will be considered as the actual return value of the method?
Try: The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block.
Catch: You associate exception handlers with a try block by providing one or more catch blocks directly after the try block. No code can be between the end of the try block and the beginning of the first catch block.
Finally: The Finally block always executes when the try block exits. This ensures that the Finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a Finally block is always a good practice, even when no exceptions are anticipated.