• A Lackey (unregistered)

    Woo-hoo! Finally, code of the same quality as my own has found it's way to WTF greatness! I always explicitly throw an exception, rather than forcing a null pointer exception, but it's roughly the same thing...

    Why would I do this? Because it workeds. Because I needed an exception for Log4j (so just using the Thread.getStackTrace wouldn't work), and because I'm too lazy to look up whether the stack trace is filled in during the creation of the Exception or the throwing.

    Just about the last thing I want stuck in my head is when the stack trace gets filled in. I'm a bit sad that I will remember it now and that I won't get that memory back...

    Anyway it works just fine. I won't even go back and change it.

  • dkf (unregistered) in reply to Lemming
    And this functionality is already present since Java 1.0.

    From the Java 1.0 API:

    public static void dumpStack()
    Prints a stack trace of the current thread. Used only for debugging. See Also: printStackTrace in class Throwable.
    These days (i.e. from 1.5 onwards) it's considered better to use Thread.getStackTrace since that makes it far easier to control what happens to that trace; you might not want to print it to System.err if you're in a headless server.
    For newcomers it's maybe a bit strange this method can't be found on class System, but that's because each thread has his own stack.
    They have to learn that sometime anyway; nobody should stay a noob forever. (Some do though, as we all know...)
  • (cs) in reply to Ed
    There is a huge difference between opening a TXT file and opening an EXE file. And, since one does not know what sort of funky things the recipient's mail client will do to the code if sent in the body of the email, such as reformatting or attempting to compile, it should be standard practice to send code as an attached TXT file.

    What's with you people? This is Alex's site, and if he wants the code that's submitted in an Excel spreadsheet in WingDings font, that's his perogative. If you don't like sending it the way he prefers, don't send it. There's plenty of bad code around.

    Damn, it's really getting old listening to all the complaining about this site. "The forum software sucks. The new name sucks. Not attaching code to submissions sucks." People who post these complaints suck.

  • (cs) in reply to A Lackey

    Nice WTF. I went 'WTF?' for real. Although that one from log4j that was mentioned earlier is even better!

  • Sgt. Preston (unregistered) in reply to Watson
    Sgt. Preston:
    Derrick Pallas:
    Sgt. Preston:
    That might make the practice inconvenient, but how is it "degrading"?

    For me: it degrades my triage and processing speeds.

    For you: your submission is being degraded to an F.

    My "submission" was a simple, honest question that hasn't really been answered. Is this slap at my question an example of your "good social skills"?

    Compensating for the shift in voice from passive to active:

    If you make a submission and your supplied code is included in an attachment then a) I'd have to deal with an added burden that offers no benefit; as a result b) I triage your submission down to priority F.


    Much. Thank you, Watson.

  • better than success (unregistered)

    The best way to get stack information for the current thread:

    http://java.sun.com/javase/6/docs/api/java/lang/Thread.html http://java.sun.com/javase/6/docs/api/java/lang/StackTraceElement.html Specifically in Thread:

    public StackTraceElement [] getStackTrace() and his friend public static Thread currentThread()

    Available since Java 5. I use this all the time to pull stack elements for logging and debugging.

    captcha: bling (aww yeah)

  • MetaDragon (unregistered) in reply to Asd
    Hey, I was once working at a software that throwed a weard exception indicating a error at Apache's Log4j... After a full day inpecting the error I found the following code:
    Logger l = null;
       l = new Logger("FileName");
    }catch(Exception e){
    Absolutely brillant. You should have submitted that.

    And no you do not need to use the return value of a method call. I can not think of a single language that requires that. Here is a tip: if you have to write "I'm not sure" when discussing basic language features then you probably should not reply at all.

    PL/SQL has functions (return something) and procedures (return nothing). If you call a function and don't assign it to a variable, the interpreter thinks you must be trying to call a procedure with that name and your code will fail.

  • Sayer (unregistered) in reply to Anonymous L0sr
    Anonymous L0sr:
    I think .NET does something like this when you ask for a stack trace--it'll throw an exception, catch it, and then give you the stack trace.

    The tell-tale "first exception thrown" slowdown is what gave it away to us.

    The .NET 'as' operator in C# works the same way. It'll try the cast and if it fails, you'll see the slowdown and get null.

    Wrong. Please stop talking if you have no clue what you're talking about.

    Please stop talking if you have no intention to enlight the unenlightened.

    No. This first step is to STFU if you don't know and use the interweb to find out. This kind of post only wastes time as people read it, think about it, research to verify it, and finally explain how/that it's wrong.

    Indeed. And who wants to waste time stamping out stupidity when it's easier to bolster your self worth by ridiculing it?

  • (cs) in reply to roe
    David Hall:
    >> this used to be the way to pull a stack trace in Java until 1.5

    nonsense. Ever since Java 0.0, you've always had the option to simply construct an exception and access its stack trace.

    log.logError(new Exception())

    has always worked.

    That is, of course, what I meant, using exceptions to get stack traces. I was referring to the null-catch stuff as means of object-construction.

    I had no idea that you do not have to throw the exception... I have done something like the example:

    try { throw new Exception("Who called me?"); } catch (Exception e) { System.out.println("I was called by " + e.getStackTrace()[1].getClassName() + "!"); }

    I was new to Java though. And it did work, I was attempting to get the name of the class that called this code, without passing the method name every time.

Leave a comment on “Bringing LOGing Back”

Log In or post as a guest

Replying to comment #:

« Return to Article