• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query with Q from Jxam

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code fragment below is provided:
1. class Test{
2. public static void main(String [] args){
3. aMethod();
4. }
5.
6. static void aMethod(){
7. try{
8. System.out.println("abcd");
9. return;
10. } finally {
11. System.out.println("1234");
12. }
13. }
14. }
This compiles.
My queries are
1) Why does it not complain about a return on line 9 when the method is null? (Is this to do with the fact the compiler looks to see what is returned - in this case nothing - and so the presence of null works like a break?)
2) Why doesn't it complain about a try without a catch?
What are the rules about including keywords in the code without actually using them?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
return is permitted without anything after it for a void.

[This message has been edited by Thomas Paul (edited June 13, 2001).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Trevor Green:
The code fragment below is provided:
1. class Test{
2. public static void main(String [] args){
3. aMethod();
4. }
5.
6. static void aMethod(){
7. try{
8. System.out.println("abcd");
9. return;
10. } finally {
11. System.out.println("1234");
12. }
13. }
14. }
This compiles.
My queries are
1) Why does it not complain about a return on line 9 when the method is null? (Is this to do with the fact the compiler looks to see what is returned - in this case nothing - and so the presence of null works like a break?)
2) Why doesn't it complain about a try without a catch?
What are the rules about including keywords in the code without actually using them?



1) If you add some return value for this method then compiler will generate an error.
2) A catch , finally or both may follow try block.
A try block must be followed by one of them.
Similarly catch , finally without try block will generate compiler error.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the JLS:
"A try statement may have catch clauses (also called exception handlers). "
may have is the operative term here.
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that.
Didn't realise the compiler error was
" 'try' without 'catch' or 'finally' " (not just "...without catch")
So, what's the point in just writing 'return' by itself, other than because you can?
reply
    Bookmark Topic Watch Topic
  • New Topic