Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
public class Hook {

  public static void main(String[] args) {
    try {
      final InputStream in = new FileInputStream("file");
        Runtime.getRuntime().addShutdownHook(new Thread() {
          public void run() {
            // Log shutdown and close all resources
            in.close();
          }
        });

       // ...
    } catch (IOException x) {
      // handle error
    } catch (FileNotFoundException x) {
      // handle error
    }
  }
}

The JVM can abort for external reasons, such as an external SIGKILL signal (UNIX) or the TerminateProcess call (Microsoft Windows), or memory corruption caused by native methods. Shutdown hooks may fail to execute as expected in such cases because the JVM cannot guarantee that they will be executed as intended.

...

The CERT C Secure Coding Standard

ERR04-C. Choose an appropriate termination strategy

The CERT C++ Secure Coding Standard

ERR04-CPP. Choose an appropriate termination strategy

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3556e89c128c6b42-a2db6f5d-4e9d4362-a59ebf0e-3f44f2b17cbc9e25e24865ce"><ac:plain-text-body><![CDATA[

[ISO/IEC TR 24772:2010

http://www.aitcnet.org/isai/]

"Termination Strategy [REU]"

]]></ac:plain-text-body></ac:structured-macro>

MITRE CWE

CWE-705, "Incorrect Control Flow Scoping"

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2d93fde53b0577cd-387e4394-46e541fc-9c1397fe-a5fa9214bdc015af49a51962"><ac:plain-text-body><![CDATA[

[[API 06

AA. Bibliography#API 06]]

[Class Runtime

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b083fe76fcdfafe7-658f1403-428e4c3b-b9a7b8b5-a1f5df4907b2d029831b71ac"><ac:plain-text-body><![CDATA[

[[ISO/IEC TR 24772:2010

AA. Bibliography#ISO/IEC TR 24772-2010]]

Section 6.46, "Termination Strategy [REU]"

]]></ac:plain-text-body></ac:structured-macro>

...