File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Couple of questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Couple of questions" Watch "Couple of questions" New topic
Author

Couple of questions

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10

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).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Thankx a lot, Marilyn
That helps
Ragu
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

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

Joined: Jul 20, 2001
Posts: 464

[/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).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couple of questions
 
Similar Threads
Exception vs return
Inheritance Question
Multithreading and synchronized methods
Mock Question Doubt...EQ+...Loops, Iterators and Exceptions
Variable shadowing