Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

help with code

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the code below why does the statement with return not compile ?
public class A {
void method() throws Exception
//{return (throw new Exception());} COMPILE ERROR
{throw new Exception();} OK
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does the compiler say ?
throw and return both make the control return to the caller method, you cannot use both in the same statement. Either you return something or you throw an exception. You can return an exception if you want to.
throw an exception:

return something:

return an Exception (which is like returning an Object but not used very much) Note that here an SomeException object is returned and not thrown, that is the method completes normally:

SO you cannot use return and throw in the same statement.
HIH
[ January 24, 2002: Message edited by: Valentin Crettaz ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use return, you are trying to return a value to the caller of your method. The type of the return value must match the return type of the method declaration.

You have declared this method as returning *nothing* to the caller, by using the void return type in your delcaration.
Then you go and lie to the compiler!! You are trying to return an object of type Exception.
You *could* do this:

But I hope you understand the difference between throwing an exception and returning a value. They are totally different mechanisms. Returning a value means the method has completed successfully and is returning execution control back to the method which invoked it. It also returns the return value to the caller.
When you throw an exception, you halt execution, and raise an exceptional condition, which triggers a transfer of control to the first catch block in the calling chain that can handle the exception.
Rob
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiler complains as following:
A.java:4: Missing term.
{return (throw new Exception());}
one more point since we are discussing this.
say an interface has one method. and i have a Class R that implements this interface. now in this class i am free to choose whatever throws i want. right ? i am asking because there are no such things when defining methods inside an interface.

Originally posted by Valentin Crettaz:
[QB]what does the compiler say ?
throw and return both make the control return to the caller method, you cannot use both in the same statement. Either you return something or you throw an exception. You can return an exception if you want to.

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark stone:
compiler complains as following:
A.java:4: Missing term.
{return (throw new Exception());}


This statement makes no sense. You either return a value, or you throw an exception. You can't do both.
either return (something);
or throw (SomeException);

Originally posted by mark stone:

one more point since we are discussing this.
say an interface has one method. and i have a Class R that implements this interface. now in this class i am free to choose whatever throws i want. right ? i am asking because there are no such things when defining methods inside an interface.


you declare the throws clause in the interface when you make a method declaration. Then when you implement the method in a class, you must adhere to the interface's method declarations, including the throws clauses you have defined. So the answer to your second question is ,no
Rob
[ January 24, 2002: Message edited by: Rob Ross ]
 
That new kid is a freak. Show him this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic