Results 1 to 4 of 4
  1. #1
    niksipandit is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default exception handling

    its not recommended that return statement should be written in finally block.. why?

    thx

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

    Default Re: exception handling

    Quote Originally Posted by niksipandit View Post
    its not recommended that return statement should be written in finally block.. why?
    Because normally there's a nasty, cold draft in those finally blocks and those poor return statements catch a cold easily.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    niksipandit is offline Member
    Join Date
    Sep 2011
    Posts
    9
    Rep Power
    0

    Default Re: exception handling

    sorry,
    i didn't get u actually....can u explain this 1 more time..

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

    Default Re: exception handling

    Java Code:
    public class Blah {
        public static void main(String args[]) {
            try {
                System.out.println(returnFinally());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        private static int returnFinally() throws Exception {
            try {
                throwsException();
                return 0;
            } catch (Exception ex) {
                throw ex;
            } finally {
                return 1;
            }
        }
    
        private static void throwsException() throws Exception {
            throw new Exception("Blah");
        }
    }
    What happens with this code?
    What should be printed out?

    That'll tell you why a return from a finally is bad.

    In essence finally blocks should be used to tidy things up before exiting a block of code, not for returning values. usually the tidying up is in the form of closing resources (database connections, file streams, that sort of thing).

Similar Threads

  1. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 10:13 AM
  2. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 06:57 PM
  3. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  4. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 03:09 PM
  5. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 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
  •