Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: throwing and returning

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default throwing and returning

    throwing a new Exception is similar with returning a value?


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: throwing and returning

    Yes and no. Throwing a new exception will exit the method like a return, however no value will be returned. Instead, the exception will propagate itself through your code until it is either caught or it hits the top and spits it out for you to read.

  3. The Following User Says Thank You to literallyjer For This Useful Post:

    chronoz13 (October 23rd, 2009)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: throwing and returning

    i forgot to clarify my question, this is regarding with an if-else structure..

    i notice it in an else if/else block.. that when i throw an exception, the compiler didnt asked me to define a return statement..

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: throwing and returning

    Throwing interrupts normal program flow. I guess in a sense it does act like a return statement, but like literallyjer said, it's looking for something to catch the exception it threw (well, technically there are other things that can be thrown, but for the most part it'll be exceptions).

    public void doIt1(Boolean something)
    {
        if (something)
        {
            throw new NullPointerException();
         }
         System.out.println("doIt1: this will never get executed");
    }
     
    public void doIt2()
    {
         doIt1(true);
         System.out.println("doIt2: this well never get executed");
    }
     
    public void doIt3()
    {
        try
         {
              doIt2();
              System.out.println("doIt3: no exception was thrown");
         }
         catch(NullPointerException e)
         {
              System.out.println("doIt3: caught a NullPointer Exception");
         }
    }

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 23rd, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: throwing and returning

    i'll keep that one !! thanks a lot world

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: throwing and returning

    Also be vary that throwing exceptions will cost a lot more than just returning a value, so for instance if you have a method which will return either true or false this is better than not returning anything or throwing an exception.

    Throwing exceptions takes time.

    // Json

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: throwing and returning

    The performance difference is almost negligible This really only applies when you're throwing a ton of exceptions, then maybe you would want to look into other ways to structure your code.

Similar Threads

  1. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM
  2. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM