Top.Mail.Ru
March 5th, 2004 - Java developers — LiveJournal
? ?

Java developers

March 5th, 2004
Image

09:24 am - Imagedplass - Is throw/catch to get stack trace slow?

Ran across some code like this today:

    public void log(String logMsg, int logFile, int priority) {
        try {
            throw new Exception(logMsg);
        } catch (Throwable e) {
            log(e, logFile, priority);
        }
    }


where there's a log(Exception, int, int) method that logs the full stack trace of an exception.

Wouldn't it be MUCH faster to just write this:

    public void log(String logMsg, int logFile, int priority) {
        Exception e = new Exception(logMsg);
        log(e, logFile, priority);
    }


This would save the throw/catch. You still have to create the exception in either case, and I think the overall effect is the same, no?
Powered by LiveJournal.com
Image