Page 1 of 2 12 LastLast
Results 1 to 20 of 30
Like Tree2Likes

Thread: try catch block issue

  1. #1
    srivempala is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default try catch block issue

    Hi all recently i faced interview on java.they asked one question like this

    try{
    return1;
    }
    catch(){
    return 2;
    }
    finally{
    return3;
    }
    when we will get out put 1 and when we get 2 and when we get 3..plz.. help me in this

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I don't think it's intended to compile, it's intended to see if the OP knows how the flow works in a try/catch/finally block.
    Which they don't seem to.

  4. #4
    srivempala is offline Member
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    i think if there may or may not be any exception the finally block will gets executed so it will return 3.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Correct.
    Whatavere happens in this case the return statement in the finally block overrides it.
    Which is why finally block should generally not return anything.
    It's considered bad practice.

  6. #6
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Hi,

    I'm write new script following:

    try{
    return1;
    }
    catch(){
    return 2;
    }
    finally{
    return3;
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Pardon?

  8. #8
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Hi,

    I'm write new script following:

    Java Code:
    try{
       <code>
       return 1;
    }
    catch(){
       return 2;
    }
    finally{
       return 3;
    }
    If <code> not exception then it's "return 1".
    If <code> exception then it's "return 2".
    And impossible "return 3"

    ^^! A good question

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Um no.
    It returns 3 everytime.

  10. #10
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    When you used "return", it's will return value at that time, and the <code> following will not effect.

    Ex:
    Java Code:
    var int rs;
    
    function int returnValue()
    {
         try 
         {
            rs = 1 + 1
            return 1;
         }
         catch(Exception ex)
         {
            return 2;
         }
         final
         {
            return 3;
         }
    }

    The function will return 1,
    If you change "rs = 1 + a" then will return 2,
    You only two case 1,2 and not case return 3.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    No.
    A finally block always executes.
    That's the purpose of it.
    And since the finally block contains a return statement, then it is that return statement that will run. Which is why it is bad practice to have a return statement in the finally block.
    Fubarable and DarrylBurke like this.

  12. #12
    bnson is offline Artemis
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    Oh,

    I try to make script and result is 3, this is new my knowledge,

    Thank you.

  13. #13
    Sheenu Gupta is offline Member
    Join Date
    Jul 2011
    Location
    Chandigarh
    Posts
    9
    Rep Power
    0

    Default

    Ist Thing that Finally never returns anything. The Question asked to you will giver error on execution.
    2nd is that if the condition in try block is correct then It will return 1 and if user does not entered correct according to catch compiler will go to catch block for execution .
    3rd is if catch block also is unable to catch the error then finally block will execute. Finally block is like default in switches.
    Last edited by Sheenu Gupta; 07-21-2011 at 01:32 PM.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    WRONG!
    Wrong wrong wrong wrong WRONG!

    Bloody hell, do you lot not bother to actually run this yourselves?

    Finally blocks always execute!

    A return in a finally block supercedes any prior return.
    Java Code:
    public class TestReturn {
        public static void main(String[] args) {
            System.out.println(testBlocks());
        }
    
        private static int testBlocks() {
            try {
                return 1;
            } finally {
                return 2;
            }
        }
    }
    Run that...you will get a '2' printed out.

    Or:
    Java Code:
    public class TestReturn {
        public static void main(String[] args) {
            System.out.println(testBlocks());
        }
    
        private static int testBlocks() {
            try {
                throw new Exception("blah");
    //            return 1;
            } catch (Exception e) {
                return 1;
            } finally {
                return 2;
            }
        }
    }
    Same here.

    Once again, in bold:
    Finally blocks always execute!

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

    Default

    This question could come up:

    Java Code:
    import java.io.IOException;
    
    
    public class T {
    
    	private static int bar() { 
    		System.out.println("returning from bar ...");
    		return 1;
    	}
    
    	private static int foo() {
    		try {
    			return bar();
    		}
    		finally {
    			return 3;
    		}
    	}
    	public static void main(String[] args) throws IOException {
    
    		System.out.println("foo: "+foo());
    	}
    }
    We all know by now that 3 is returned (from the finally block) but is bar() evaluated?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I think yes, but I'd have to run it (as with all other edge cases, because I never keep this stuff in my head).

    Oh, there is an exception to my rant above...System.exit().

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

    Default

    Quote Originally Posted by Tolls View Post
    I think yes, but I'd have to run it (as with all other edge cases, because I never keep this stuff in my head).

    Oh, there is an exception to my rant above...System.exit().
    System.exit()? Where? In the finally block? My question still stands then ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Sorry, that was a bit of a non sequitur.

    Java Code:
            try {
                System.exit(-111);
            } finally {
                System.out.println("finally");
            }
    The finally isn't executed in this case.
    I said (rather loudly above) that they're always executed, but that's not true in this case.

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

    Default

    Quote Originally Posted by Tolls View Post
    Sorry, that was a bit of a non sequitur.

    Java Code:
            try {
                System.exit(-111);
            } finally {
                System.out.println("finally");
            }
    The finally isn't executed in this case.
    I said (rather loudly above) that they're always executed, but that's not true in this case.
    Ah, yes, all bets are off after/when System.exit( ... ) executes because the entire JVM will be no more after that method has done its ugly deed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Sheenu Gupta is offline Member
    Join Date
    Jul 2011
    Location
    Chandigarh
    Posts
    9
    Rep Power
    0

    Default

    Tolls
    &
    JasAH

    Hey Anyways I will check this thoroughly and get to you both again. Thanks for making me think that I have to clear my this concept.
    Last edited by Sheenu Gupta; 07-22-2011 at 01:37 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:32 PM
  2. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  3. System.exit() in catch block.
    By new_2_java in forum Advanced Java
    Replies: 8
    Last Post: 06-24-2008, 03:45 PM
  4. Try Catch block issues
    By kewlgeye in forum New To Java
    Replies: 11
    Last Post: 04-29-2008, 07:10 AM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM

Posting Permissions

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