Motivation
It is very common to write code similar to the listing below when using resources that might throw exceptions in garbage-collected languages:
Resource resource = null; try { resource = getResource; // Attempt to acquire the resource. // Perform actions with the resource. ... } finally { if (resource != null) // Resource might not been acquired, or already freed resource.Dispose; }The try...finally
construct is necessary for proper exception safety, since the finally
block enables execution of cleanup logic regardless of if an exception is thrown or not in the try
block.
One disadvantage of this approach is that it requires the programmer to explicitly add cleanup code in a finally
block. This leads to code size bloat, and failure to do so will lead to resource leakage in the program.
Read more about this topic: Dispose Pattern
Famous quotes containing the word motivation:
“Self-determination has to mean that the leader is your individual gut, and heart, and mind or were talking about power, again, and its rather well-known impurities. Who is really going to care whether you live or die and who is going to know the most intimate motivation for your laughter and your tears is the only person to be trusted to speak for you and to decide what you will or will not do.”
—June Jordan (b. 1939)