Results 1 to 7 of 7

Thread: finally clause

  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default finally clause

    You can do anything inside a finally clause that you can do elsewhere, including executing break, continue, or return statements, or throwing exceptions. Such actions inside a finally clause, however, can have some surprising effects. For example, consider a finally clause that is entered because of an uncaught exception. If the finally clause executes a return, the method would complete normally via the return, not abruptly by throwing the exception. The exception would have in effect been handled by the finally clause instead of a catch clause.
    From: Exceptions in Java
    Hello
    What does this text want to tell?
    Please example.
    With thanks

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

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    What does this text want to tell?
    Please example.
    With thanks
    If a finally clause returns, no exception (that wasn't handled) will be thrown; e.g.

    Java Code:
    void noException() {
       int[] a= null;
       try {
          a[42]= 54; // this throws an Exception
       }
       finally {
          return; // but it's muffled away here
       }
    }
    kind regards,

    Jos

  3. #3
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I run following program and got this error:
    Exception in thread “main” java.lang.NullPointerException:
    At Test1.noException(Test1.java:8)
    At Test1.main(Test1.java:17)
    Java Code:
    class Test1
    {
    	void noException() 
    	{
    		int[] a= null;
        try 
        {
        	a[42]= 54; // this throws an Exception
        }
        finally 
        {
        	[b]//return; // but it's muffled away here[/b]
        }
    }
      public static void main(String[] args)
      {
      	new Test1().noException();
      }
    }
    But I did not get above error with return in finally clause.
    It is very wonderful! Thank you.


    My new question:
    I am sorry. I do not know what the example can I write with this quote? Please guide me.
    consider a finally clause that is entered because a return true; statement was executed inside the try block. If the finally clause executes a return false; statement, the method will return false.

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

    Default

    Quote Originally Posted by arefeh View Post
    My new question:
    I am sorry. I do not know what the example can I write with this quote? Please guide me.
    A finally clause is always executed last, just before any optional return statement in a try block, e.g.

    Java Code:
    boolean returnTrue() {
       try {
          return 1 > 0; // this is supposed to return true
       }
       finally {
          return false; // but false is returned instead
       }
    }

    kind regards,

    Jos

  5. #5
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    How wonderful!
    This method returns false and I saw this result.


    New question:
    We know that with existing return in a method or in a constructor, we will exit from this method or this constructor.
    For example:
    void a()
    {
    System.out.println("This is a method");
    return;
    }

    Then, why with existing return in r_eturn method, we can not exit from r_eturn method?

    Java Code:
      void r_eturn()
      {
      	try
      	{
      		return;
      	}
      	finally
      	{
      		System.out.println("OK");
      	}
      }

  6. #6
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    it seemes that return statment cause we exit from a block.
    this block can be a method, an if, an else, a try or finally.
    OK?

  7. #7
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Oh!I was not true.
    I run following program and I saw that control exit from the method.
    Java Code:
      void r_eturn()
      {
      	if(1>0)
      	{
      		System.out.println("True");
      		[b]return;[/b]
      	}
      	System.out.println("This is a method");
      }
    Then, finally clause must run.

Similar Threads

  1. Mysql LIKE clause problem
    By stalkerism in forum JDBC
    Replies: 1
    Last Post: 07-31-2009, 10:01 AM
  2. How to use Finally
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM
  3. Try---finally
    By javarishi in forum New To Java
    Replies: 14
    Last Post: 04-09-2008, 10:34 AM
  4. Using escape sequence with like clause (%)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:57 AM
  5. Using escape sequence with like clause (_)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 09:55 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
  •