...
| Code Block | ||
|---|---|---|
| ||
public class ExceptionLog {
private static String logMessage;
public static void main(String[] args) {
ExceptionLog log = new ExceptionLog();
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw = new FileWriter("log_file.txt", true);
//this This can throw an exception, but the logging of messages is
// not silently prevented.
bw = new BufferedWriter(fw);
}
catch (IOException e) {
// The logging example cannot recover from failure to open
// the log file.
throw new RuntimeException(e);
}
//some security exception occurs here
try {
log.logMessage("Security Exception has occurred!");
log.writeLog(bw);
}
catch (IOException e) {
// Logging of the security exception does not silently fail.
System.err.println("Logging error failed.");
}
try {
bw.close();
}
catch (IOException e) {
System.err.println("Closing log file failed.");
}
}
public static void logMessage(String message) {
logMessage = message;
}
public void writeLog(BufferedWriter bw) throws IOException {
bw.write(logMessage + "\n");
System.err.println(logMessage);
}
}
|
...