Results 1 to 7 of 7
Thread: finally clause
- 01-30-2010, 02:14 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
finally clause
HelloYou 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
What does this text want to tell?
Please example.
With thanks
- 01-30-2010, 02:29 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 01-30-2010, 04:39 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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)
But I did not get above error with return in finally clause.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(); } }
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.
- 01-30-2010, 04:54 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-30-2010, 06:35 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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"); } }
- 01-30-2010, 08:47 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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?
- 01-30-2010, 09:11 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Similar Threads
-
Mysql LIKE clause problem
By stalkerism in forum JDBCReplies: 1Last Post: 07-31-2009, 10:01 AM -
How to use Finally
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:40 PM -
Try---finally
By javarishi in forum New To JavaReplies: 14Last Post: 04-09-2008, 10:34 AM -
Using escape sequence with like clause (%)
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 09:57 AM -
Using escape sequence with like clause (_)
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks