Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree1Likes

Thread: Exceptions prints all weird -_-

  1. #1
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Angry Exceptions prints all weird -_-

    this is my code for exceptions, its really simple b/c i just started learning about this. don't laugh at me bro xD
    Java Code:
    package exception;
    
    public class Exception {
    
        public static void main(String[] args) {
            
            try
            {
                int [] array = new int[-5];
            }
            
            catch(NegativeArraySizeException e)
            {
                System.out.println("NegativeArraySizeException caught");
                e.printStackTrace();
                System.out.println("");
            }
            
            try 
            {
                int negativeSize = -5;
                int [] array = new int[5];
                for(int i = -1; i < array.length; i++){
                    array[i] = negativeSize;
                }
            }
            
            catch(IndexOutOfBoundsException e)
            {
                System.out.println("IndexOutOfBoundsException caught");
                e.printStackTrace();
                System.out.println("");
            } 
            
           try
            {
               String s = null;
               System.out.println(s.length());
            }
            
            catch(NullPointerException e)
            {
               System.out.println("NullPointerException caught");
               e.printStackTrace();
               System.out.println("");
            } 
            
        }
    }
    this is the weird output:
    Java Code:
    NegativeArraySizeException caught
    java.lang.NegativeArraySizeException
    
    IndexOutOfBoundsException caught
    
    NullPointerException caught
    
    	at exception.Exception.main(Exception.java:13)
    java.lang.ArrayIndexOutOfBoundsException: -1
    	at exception.Exception.main(Exception.java:28)
    java.lang.NullPointerException
    	at exception.Exception.main(Exception.java:42)
    BUILD SUCCESSFUL (total time: 0 seconds)
    ITS ALL WERIDDDDDDDD!!!!!!!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    What is so 'werid' about it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Exceptions prints all weird -_-

    hy josAH why all the stack traces are printed at the last ? they shouldn't be in order ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    Quote Originally Posted by allaudin View Post
    hy josAH why all the stack traces are printed at the last ? they shouldn't be in order ?
    The first two Exceptions didn't generate a stack trace because the Exeption happened in the code shown; only the last one generates a stack trace.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Exceptions prints all weird -_-

    The stack trace prints to System.err -- not System.out. The two aren't synchronized.

    Print your custom messages to System.err and everything will be in the order that you apparently expect.

    And I agree with Jos -- there's nothing 'weird' about the output you've shown.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,988
    Rep Power
    5

    Default Re: Exceptions prints all weird -_-

    I have to wag the finger here: calling things "weird" or "strange" is a defense mechanism against admitting you don't understand it. It proves one simple thing: what you think is right simply isn't. You have to auto-correct that behavior and that doesn't stop even when you become an experienced developer. I regularly have to slap a colleague on the wrist and they have to slap me on the wrist when we yet again get an exception we've never seen before and we start to blame the JVM, the OS, the position of the stars and the mountain of APIs we're using. NO! Its a bug in the code or a bug in our understanding of the situation; find it, fix it.

    Usually it is something so silly you don't even consider thinking about it.
    wsaryada likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Exceptions prints all weird -_-

    ur right! im gonna go back and review this concept again. "calling things "weird" or "strange" is a defense mechanism against admitting you don't understand it." - epic quote by gimbal2

  8. #8
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Exceptions prints all weird -_-

    i still don't know why it prints the way it is, but it doesn't matter right now because i need to learn more about try and catch. I am focusing at the wrong place xD
    Anyways, thanks for all of your help!

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Exceptions prints all weird -_-

    Quote Originally Posted by HelpingIsCaring View Post
    i still don't know why it prints the way it is
    I guess that's because I didn't tell you at #5
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    Invisible ink, that's what this forum is suffering from, I'm telling you ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Exceptions prints all weird -_-

    Hi Jos, why the blank post?
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    Did I hear something? Nah, must've been my imagination ...
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,988
    Rep Power
    5

    Default Re: Exceptions prints all weird -_-

    I'm so cool, I can read invisible ... fonts.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    Quote Originally Posted by gimbal2 View Post
    I'm so cool, I can read invisible ... fonts.

    No you can't.

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,988
    Rep Power
    5

    Default Re: Exceptions prints all weird -_-

    Yes I can. Unfortunately the 'what's new?' page destroyed your otherwise perfectly fine joke ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    Quote Originally Posted by gimbal2 View Post
    Yes I can. Unfortunately the 'what's new?' page destroyed your otherwise perfectly fine joke ;)
    Maybe you didn't know it yet, but the person who presses that button has to buy us all a beer (Grolsch please ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,988
    Rep Power
    5

    Default Re: Exceptions prints all weird -_-

    Did I hear something? Nah, must've been my imagination...
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    Otherwise we'd like you to meet our Rotating Knives Machine (tm)

    kind regards,

    Jos (<-- very nice guy)
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,988
    Rep Power
    5

    Default Re: Exceptions prints all weird -_-

    You can't trademark that, Nine Inch Nails already did that long ago as part of the (banned) music video for their Happiness In Slavery song.

    But fine, if you're going to send threats... PM me your address and I'll send you a case. Don't worry if its ticking, modern bombs don't tick.

    Erik ( <-- loves to quote movies )
    Last edited by gimbal2; 08-05-2013 at 09:54 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default Re: Exceptions prints all weird -_-

    You are not oppressing me are you? Yes you are; see? He's oppressing me! Help! Help!

    Jos (<-- not afraid at all, not me ;-)

    ps. I'd prefer those bottles with the ceramic top.
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Array prints line 2x
    By Cinnic in forum New To Java
    Replies: 6
    Last Post: 07-14-2013, 03:22 PM
  2. Make a loop that prints the lastnumber only
    By ramin in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 03:11 AM
  3. Txt to ArrayList only prints []
    By Quasi Urbane in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 07:16 PM
  4. Canvas Prints
    By canvas02 in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-18-2011, 05:33 PM
  5. Count each cell as it prints.
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 10-29-2010, 12:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •