enfrptes

sendassignment@tutorspoint.com

Finally Keyword Homework Help

Tutospoint programming assignment expert provides assignment writing help services in topics of finally keyword homework help.

Definition of Finally keyword:


The finally keyword ensures that all the statements present inside the block finally block is compulsory executed.

What is the need to finally block?

Sometimes, some statements inside a try block need to be executed compulsorily whether exceptions occur or not.

Example:

Try

{

Get connected to a printer

=======

Con .close ()

}

Catch (Exception e)

{

System. Out. Prinln(e);

}

Explanation:

a) Now after getting a connection to a printer, let us assume that an exception is generated in one of the statements after getting the connection. Then the control comes out of the trial without executing the rest of the statements.

b) The last statement inside the try block icon. Close (). This statement will not be executed if an exception occurs before this.

c) Now the scenario is, the resource (printer) has been allocated to our system, but it is not released. This is infective resource management.

d) To overcome this problem, many people suggest one solution i.e. to write con. Close () outside try to catch. Not only con. Close (), but any statement which needs to be compulsorily executed is supported to be placed outside try-catch.

e) In fact, this solution works well, but it is not at all recommended.

Programming Assignment Help
In order to maintain modularity, all the statements present intryblock that are to be compulsorily executed irrespective of expressions, are to be placed in a separate block finally.

g) Finally is a block associated with the try block just as the catch block.

h) After the execution of either try or catch blocks, it then executes the finally block and then goes for the rest of the statements present in the function, outside the try-catch.

i) Finally block cannot exist individually without a try block.

j) A try block can have any number of catch blocks but it can have only one finally block.