what is the best practice for logging runtime error or uncheked exceptions
hello
my main problem is logging the nullPointerException to a file
suppose I have this method
publicvoid myMethod()
{
//..... some declaration here
User user = obj.findUser("userx");//this may return null
System.out.println("user name is "+user.getName());// I may have a null pointer exception here
}
so what is the best practice to catch the exception ?
can I log the exception without catching it ?
thank you
Strictly for the logging to a file part , you can use org.apache.log4j.Logger.
Isnt there something like
e.printStackTrace(InputStream arg1)
And you just put the input stream for the file in there...
public void method() {
try {
// code that may cause exception
} catch (Exception e) {
e.printStackTrace(InputStream);
}
}
> Isnt there something like
>
> e.printStackTrace(InputStream arg1)
>
> And you just put the input stream for the file in
> there...
>
> > public void method() {
> try {
> // code that may cause exception
> } catch (Exception e) {
> e.printStackTrace(InputStream);
> }
> }
A terrible way of logging exceptions. Use a logging framework as the other poster suggested, such as Log4J or the one the JDK ships with more recently
> A terrible way of logging exceptions.
Not that im not agreeing with you, but why? (I havent actually used this)
> > A terrible way of logging exceptions.
>
> Not that im not agreeing with you, but why? (I havent
> actually used this)
Since you usually want to log more information than just the exception (e.g. timestamp, current thread etc).
You usually also want to be able to control maximum log file size etc.
Kaj
kajbja at 2007-7-29 17:24:14 >

> so what is the best practice to catch the exception
> ?
I would never call user.getName without checking for null if the method can return null.
> can I log the exception without catching it ?
No, but you can re-throw if after logging.
Kaj
kajbja at 2007-7-29 17:24:14 >

> > A terrible way of logging exceptions.
>
> Not that im not agreeing with you, but why? (I havent
> actually used this)
Because it's always on, for one thing. It's not really configurable either, unless you go to some trouble to make it so. You'd have to provide an InputStream. How? Either at compile-time, which is undesirable, or by providing configuration, which a logging framework has already done, better. Then there's the fact that you can't log anything other than the stacktrace - not particularly helpful a lot of the time. In short, it's a buggy and incomplete solution to something that's already been solved much much better by, for example, Log4J
ok thank you
Im using logger from java.util.logging.Logger
Runtime Errors and UnChecked Exceptions should be considered as bugs.
Find why you have a null reference for an object and make necessary changes in your logic to avoid that.
> Runtime Errors and UnChecked Exceptions should be
> considered as bugs.
>
> Find why you have a null reference for an object and
> make necessary changes in your logic to avoid that.
that is what i was searching for
thanks