catching exceptions in a catch block

  • Thread starter crazy fo sheezy
  • Start date
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
 
M

Mike Schilling

crazy said:
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.

You have to accept that you can't easily log errors caused by being
unable to write to the log file. Given this, you can write a logging
class:

public class Log
{
private String logFile;

public void logException(Exception ex)
{
try
{
PrintWriter pw = new PrintWriter(new
BufferedWriter(new FileWriter(logFile));
e.printStackTrace(pw);
pw.close();
}
catch (Exception ex)
{
; // what can you do?
}
}
}

and use this class to do all your logging. if you decide to, say,
have another file that collects all first-level logging errors, append
to it in the catch block (and ingore errors writing to it.) What
might work even better is to use a logging framework that handles all
of this for you, say java.util.logging.
 
Joined
Jun 9, 2009
Messages
5
Reaction score
0
As already indicated, a better way is to open the file much before you really direct all other business exceptions to that file.

During the initial file opening fails, it is better to stop program or say that users to know what next could be done..

Don't mix many functionalities with in a single place.

Regards,
Raja Nagendra Kumar,
C.T.O
tejasoft.com
-Java Product Development, Refactoring and Unit Testing Specialists
 
W

whatchamakeofit

If I end up creating a separate logging class, then don't I run into
the same problem? I've also tried using the Logger API, but that
doesn't seem to work either. The Logger API could still throw an
exception if I'm still logging everything to a physical log file,
rather than standard out (ie. the console). I would imagine this to
be a pretty common problem so there's gotta be a solution to this
somewhere, right? :p
 
M

Mike Schilling

whatchamakeofit said:
If I end up creating a separate logging class, then don't I run into
the same problem?

As I said, at some point you need to accept that you can't log the
inability to log.
 
R

Roedy Green

} catch (Exception e) {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("c:\
\foo.log")));
e.printStackTrace(pw);
pw.close();
}

how about
finally { pw.close; }
--
Roedy Green Canadian Mind Products
http://mindprod.com

Never discourage anyone... who continually makes progress, no matter how slow.
~ Plato 428 BC died: 348 BC at age: 80
 
D

Daniel Pitts

crazy said:
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 not to reinvent the logging wheel. look at log4j, commons-logging
or even Java Logging (in that order).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top