plz tell me the reason of compilation error

hi...plz tell me why the following code is giving compilation error...it should execute the catch block and then finally block should ve overwritten the return value....

public class Test2{

public static void main(String args[]){

System.out.println(method());

}

public static int method(){

try{

throw new Exception();

return 1;

}

catch(Exception e){

return 2;

}

finally{

return 3;

}

}

}

[495 byte] By [biswa_orkuta] at [2007-11-27 7:07:51]
# 1
Well, you're throwing an Exception from a method that hasn't declared "throws Exception" and your return 1; is unreachable code.Now stop writing these questions here and read a Java book or something.
-Kayaman-a at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 2

> Well, you're throwing an Exception from a method that

> hasn't declared "throws Exception" and your return 1;

> is unreachable code.

>

> Now stop writing these questions here and read a Java

> book or something.

i have to agree. you've exceeded your limit on questions.

but seriously all these questions suggest that you need to learn the basics. get a book or spin your wheels. your choice.

petes1234a at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 3

> Well, you're throwing an Exception from a method that hasn't declared "throws

> Exception"

No. There is no Exception being thrown from the try statement - for the very reason you gave in the other thread. The try statement will return 3 with no Exception. (This true whatever the try block might do.)

> and your return 1; is unreachable code.

Yes - it is giving a compilation error for this very reason. Or to be more accurate it is giving a compilation message that says this very thing.

> Now stop writing these questions here and read a Java book or something.

Agreed. And stay away from finally blocks that don't (in Eclipse's words) "complete normally" ie have a return, break, continue etc or throw an exception.

pbrockway2a at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 4
>Well, you're throwing an Exception from a method that hasn't declared "throws Exception" and your return 1; is unreachable code.I am handling the exception in the method itself...do i need to throw the exception in that case?...
biswa_orkuta at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 5

> I am handling the exception in the method itself...do i need to throw the exception

> in that case?...

Doesn't matter. You could take this bit out:catch(Exception e) {

return 2;

}

and it still wouldn't need "throws Exception". Because that try statement (the combined try/catch/finally blocks) does not throw an Exception. It returns a 3.

pbrockway2a at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 6

>Doesn't matter. You could take this bit out:

>catch(Exception e) {

>return 2;

>}

>and it still wouldn't need "throws Exception". Because that try >statement (the combined try/catch/finally blocks) does not throw an >exception. It returns a 3.

Thank you all...i think i got it....the compilation error is due to the fact that return statement is unreachable in try block....that means compiler finds that if it throws an exception from try block it can not execute the return statement in try block...so the compiler is giving error...

In the book by kathy Sierra, these cases are not discussed clearly....plz suggest me another good book which has dealt with all these....in fact, i m doing all these as a part of my preparation for a java exam....but unfortunately JVM is not installed in the machine i m using now....i need some formalities to be completed for its installation here...i m telling this coz in some cases i feel if it would be installed here i could write my test code to find out the reason....

biswa_orkuta at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 7
Ah yea, of course the throws Exception doesn't matter, I wasn't thinking clearly.I have been going through absolutely atrocious code from a co-worker all morning and it's affecting my mental health.(I'm serious, the code is worse than you can imagine)
-Kayaman-a at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 8

Thank you kayaman for your kind helps in different threads....i want to improve java basics...kindly suggest a good book which covers various tricky aspects....i m not very old in this technology....but i found you ve a very sound knowledge...how many years are you working with java...and plz tell me about some good books and sites...

biswa_orkuta at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 9
I haven't read many books, but I've been on the forums for 6 years or so now. You can learn a lot of the tricky stuff from here.It's a shame there's also a lot of misinformation going on here, I probably wouldn't trust anyone under 1000 posts.
-Kayaman-a at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...
# 10
ok...thank you...i joined this 5 days back...i ll be in touch of this forum regularly....thank you again
biswa_orkuta at 2007-7-12 18:59:17 > top of Java-index,Java Essentials,Java Programming...