• 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

Couple of questions

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
static void main(String[] args)
{
int j = 1;
try
{
long i = doIt() / (j = 2);
} catch (Exception e)
{
System.out.println(" j = " + j);
}
}
public static long doIt() throws Exception {
throw new Exception("FORGET IT"); }
}
This method doIt() returns long... but there is no return method
It compiles and runs... I dont get it... I am missing something here ...helpme

From JQ plus:
One of the question's answer key says..
"
A valid declaration of the main ( ) method must be public and static, should return void and should take a single array of Strings.
The order of the static and public keywords is irrelevant. But return type should always come just before the method name.
'final' does not change the method signature.
Also, in most of the JDK environment, even a private or protected main() method works but for the purpose of exam, main() method should be only public."
For the purpose of the exam, main() should be only public??
Where did this come from?
Is it like a hidden rule or something... or I am missing something?
Please help
Ragu
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a return were either before or after throw, the last of them would be unreachable.
I think the JLS would be our bible for the exam Ragu.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou ...
But unreachability is not my issue.
Compilation is my issue...
public static long foo(){} //this wont compile
public static long doIt() throws Exception { //this does Compile
throw new Exception("FORGET IT"); }
}
So the return value is ignored since its thrown before its being able to return some value for itself?
(ie its contract terminates early before its completely done?
(so any return value is == void??? is an exception is thrown in the middle of the code!!!)
Can anyone help me please?
Thankx in advance
Ragu

[This message has been edited by Ragu Sivaraman (edited December 02, 2001).]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1 is assigned to j
doIt() throws an exception before 2 is assigned to j
Therefore j = 1 is printed when the catch catches the thrown Exception (since j has not changed since being initialized)
I added another line to the catch block so you can see the exception is caught.
I also added a line after the catch block so you can see that execution continues because the exception is caught.

" The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader. The Java virtual machine then links the initial class, initializes it, and invokes its public class method (static) void main(String[]). The invocation of this method drives all further execution. "

Also refer to the discussion here for more on this subject.
[This message has been edited by Marilyn deQueiroz (edited December 02, 2001).]
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankx a lot, Marilyn
That helps
Ragu
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This method doIt() returns long... but there is no return method
It compiles and runs... I dont get it... I am missing something here ...helpme


Reachability is the answer you were asking for here because the compliler won't tolerate an unreachable statement:

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[/b]
</BLOCKQUOTE>

I will explain my concern again!
public static long foo(){} //this wont compile
It wont compile becoz there is no long value returned. ie in other words the foo() method's contract is not over yet or not implemented properly to satisfy the signature..
NOW..
public static long doIt() throws Exception { //this does Compile
throw new Exception("FORGET IT"); }
}
The same scenario, ie incomplete implementation/pre-mature completion of the contract ( ie no "return 2L" ) but still compiles and runs,ie javac ignores the return datatype in its signature..now why? It's becoz it is forced to ignore since it wont even compile if there are any statements after the throw stmt (which includes return too..) Now my question is Wouldn't be reasonable to expect that the compiler must give a (compilation error which may make the compiler as too unforgiving or strict) or atleast a warning that since any statement is unreachable after a throw statement so as the return expectancy..so the method signature's return commitment is ignored?
I hope somebody understands my problem
And please correct me or enlighten me
Thank in advance
Ragu

[This message has been edited by Ragu Sivaraman (edited December 03, 2001).]
 
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
Ragu,
a return or throw statement are consider as statement termination regardless whether the method should return a value or not.
From JLS 14.1


...
If such an event occurs, then execution of one or more statements may be terminated before all steps of their normal mode of execution have completed; such statements are said to complete abruptly.
An abrupt completion always has an associated reason, which is one of the following:
A break with no label
A break with a given label
A continue with no label
A continue with a given label
A return with no value
A return with a given value
A throw with a given value, including exceptions thrown by the Java virtual machine
The terms "complete normally" and "complete abruptly" also apply to the evaluation of expressions (�15.6). The only reason an expression can complete abruptly is
that an exception is thrown, because of either a throw with a given value (�14.17) or a run-time exception or error (�11, �15.6).
If a statement evaluates an expression, abrupt completion of the expression always causes the immediate abrupt completion of the statement, with the same reason. All
succeeding steps in the normal mode of execution are not performed.


In clear, an exception is also capable of returning the ocntrol to the caller method exactly as the return does... If some condition is not met and no value can be returned, then an Exception should be thrown and the caller should take appropriate measures.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Crettaz:
Ragu,
a return or throw statement are consider as statement termination regardless whether the method should return a value or not.
From JLS 14.1
In clear, an exception is also capable of returning the ocntrol to the caller method exactly as the return does... If some condition is not met and no value can be returned, then an Exception should be thrown and the caller should take appropriate measures.
HIH



Why worry when val is here!!!
..... Thankx a lot .. Val ...I appreciate it
ragu
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic