Suppose there are two functions, Function First allocates a memory and then call Function Second..... But in Function Second Exception occurs!!! How do you notify Function First about this in order to deallocate the memory allocated by First???
Use the try catch block the function second
or
Try finally which is in java (c++ extended by Microsoft in c++)
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. Executed when the program finished, because it is a keyword
finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method. Executed when the object is deleted, because it is a class member function
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment