C
crazy fo sheezy
Hi. I have this piece of code here:
try {
....
} catch (Exception e) {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("c:\
\foo.log")));
e.printStackTrace(pw);
pw.close();
}
The problem I'm having is that the code in the catch block could throw
an IOException, FileNotFound for example. I want to be able to log
all errors in a log file. So, my first thought was to try to wrap
everything in the catch block within its own try/catch. But then,
since I want to log all errors to a log file, I'll need to instantiate
another PrintWriter in the second catch block (see below). If I keep
going, then I just end up with a bunch nested try/catches with no end
in sight.
try {
....
} catch (Exception e) {
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("c:\
\foo.log")));
e.printStackTrace(pw);
pw.close();
} catch (IOException e) {
//another PrintWriter????
}
}
I'm pretty sure this is the wrong way to accomplish what I want to
do. Could someone help? Thanks
try {
....
} catch (Exception e) {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("c:\
\foo.log")));
e.printStackTrace(pw);
pw.close();
}
The problem I'm having is that the code in the catch block could throw
an IOException, FileNotFound for example. I want to be able to log
all errors in a log file. So, my first thought was to try to wrap
everything in the catch block within its own try/catch. But then,
since I want to log all errors to a log file, I'll need to instantiate
another PrintWriter in the second catch block (see below). If I keep
going, then I just end up with a bunch nested try/catches with no end
in sight.
try {
....
} catch (Exception e) {
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("c:\
\foo.log")));
e.printStackTrace(pw);
pw.close();
} catch (IOException e) {
//another PrintWriter????
}
}
I'm pretty sure this is the wrong way to accomplish what I want to
do. Could someone help? Thanks