A java method usually consists of the following sections:
Preparation: Checking of parameters and preparing to do work
Work: Do some processing or calculation.
Cleanup: Closing streams, returning results.
public static synchronized void forceAuditLog(String message)
{
PrintWriter pw = null;
OutputStream out = null;
try
{
// 1) Preparation, create the objects
out = new FileOutputStream("audit.log",true);
pw = new PrintWriter(out);
// 2) Work, use the objects
String caller = getCaller();
pw.println(System.currentTimeMillis()
+ " [" + caller + " ] "
+ message);
}
catch (IOException e)
{
// Dealt with transparently by framework
throw new AuditException(e);
}
finally
{
// 3) Clean up the mess left behind by the objects
if (pw != null) {pw.flush(); pw.close();}
if (out != null) try {out.close();} catch (IOException e){}
}
}