Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  


AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Performing cleanup with finally: Whatís finally for?

java

+ Font mai mare | - Font mai mic



DOCUMENTE SIMILARE

Trimite pe Messenger
Toolkit, Window, Container, And Events
Using imports to change behavior
Connecting Java to CGI
Using Maps
Initializing the base class
Remote methods: Remote interfaces
Typical uses of IO streams: Input streams
Introduction to objects
A compendium of operators
Inheritance syntax


Performing cleanup
with finally

Thereís often some piece of code that you want to execute whether or not an exception occurs in a try block. This usually pertains to some operation other than memory recovery (since thatís taken care of by the garbage collector). To achieve this effect, you use a finally clause at the end of all the exception handlers. The full picture of an exception-handling section is thus:




try catch (A a1) catch (B b1) catch (C c1) finally

To demonstrate that the finally clause always runs, try this program:

//: FinallyWorks.java

// The finally clause is always executed

public class FinallyWorks catch(Exception e) finally

}

}

} ///:~

This program also gives a hint for how you can deal with the fact that exceptions in Java (like exceptions in C++) do not allow you to resume back to where the exception was thrown, as discussed earlier. If you place your try block in a loop, you can establish a condition that must be met before you continue the program. You can also add a static counter or some other device to allow the loop to try several different approaches before giving up. This way you can build a greater level of robustness into your programs.

The output is:

Exception thrown

in finally clause

No exception

in finally clause

Whether an exception is thrown or not, the finally clause is always executed.

Whatís finally for?

In a language without garbage collection and without automatic destructor calls, finally is important because it allows the programmer to guarantee the release of memory regardless of what happens in the try block. But Java has garbage collection, so releasing memory is virtually never a problem. Also, it has no destructors to call. So when do you need to use finally in Java?

finally is necessary when you need to set something other than memory back to its original state. This is usually something like an open file or network connection, something youíve drawn on the screen or even a switch in the outside world, as modeled in the following example:

//: OnOffSwitch.java

// Why use finally?

class Switch

void on()

void off()

}

public class OnOffSwitch catch(NullPointerException e) catch(IllegalArgumentException e)

}

} ///:~

The goal here is to make sure that the switch is off when main( ) is completed, so sw.off( ) is placed at the end of the try block and at the end of each exception handler. But itís possible that an exception could be thrown that isnít caught here, so sw.off( ) would be missed. However, with finally you can place the closure code from a try block in just one place:

//: WithFinally.java

// Finally Guarantees cleanup

class Switch2

void on()

void off()

}

public class WithFinally catch(NullPointerException e) catch(IllegalArgumentException e) finally

}

} ///:~

Here the sw.off( ) has been moved to just one place, where itís guaranteed to run no matter what happens.

Even in cases in which the exception is not caught in the current set of catch clauses, finally will be executed before the exception-handling mechanism continues its search for a handler at the next higher level:

//: AlwaysFinally.java



// Finally is always executed

class Ex extends Exception

public class AlwaysFinally finally

} catch(Ex e) finally

}

} ///:~

The output for this program shows you what happens:

Entering first try block

Entering second try block

finally in 2nd try block

Caught Ex in first try block

finally in 1st try block

The finally statement will also be executed in situations in which break and continue statements are involved. Note that, along with the labeled break and labeled continue, finally eliminates the need for a goto statement in Java.

Pitfall: the lost exception

In general, Javaís exception implementation is quite outstanding, but unfortunately thereís a flaw. Although exceptions are an indication of a crisis in your program and should never be ignored, itís possible for an exception to simply be lost. This happens with a particular configuration using a finally clause:

//: LostMessage.java

// How an exception can be lost

class VeryImportantException extends Exception

}

class HoHumException extends Exception

}

public class LostMessage

void dispose() throws HoHumException

public static void main(String[] args)

throws Exception finally

}

} ///:~

The output is:

A trivial exception

at LostMessage.dispose(LostMessage.java:21)

at LostMessage.main(LostMessage.java:29)

You can see that thereís no evidence of the VeryImportantException, which is simply replaced by the HoHumException in the finally clause. This is a rather serious pitfall, since it means that an exception can be completely lost, and in a far more subtle and difficult-to-detect fashion than the example above. In contrast, C++ treats the situation in which a second exception is thrown before the first one is handled as a dire programming error. Perhaps a future version of Java will repair the problem. (The above results were produced with Java 1.1.)



C++ exception handling does not have the finally clause because it relies on destructors to accomplish this sort of cleanup.

A destructor is a function thatís always called when an object becomes unused. You always know exactly where and when the destructor gets called. C++ has automatic destructor calls, but Delphiís Object Pascal versions 1 & and 2 do not (which changes the meaning and use of the concept of a destructor for that language).






Politica de confidentialitate



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 427
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site