This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Natr... mockQ 19 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Natr... mockQ 19" Watch "Natr... mockQ 19" New topic
Author

Natr... mockQ 19

madhuri vl
Ranch Hand

Joined: Jan 17, 2001
Posts: 86
class MyException extends Exception{
2. MyException(Object x){}
3. }
4.
5. public class Q19{
6. static void call(){
7. try{
8. try{
9. throw new MyException("");
10. }
11. finally{
12. return;
13. }
14. }
15. catch(MyException e){
16. System.out.println(e.getClass());
17. }
18. }
19.
20. public static void main(String[] args){
21. call();
22. }
23. }
a. Compile time Error at line 9.
b. Compile time Error at line 12.
c. Compile time Error at line 15.
d. Runt time Error at line 15.
e. Run time Error at line 16.
f. Compiles and run successfully.
Can anybody explain?
Thanks in bunches,
Madhuri.


madhuri.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Explain what?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Andy Boyibolomy
Greenhorn

Joined: Feb 12, 2001
Posts: 11
I think the answer is C.
line 12: return;
so the following catch block is unreachable in any case, the compiler will complain then.

------------------

Have a good J Day
Andy


A.B.
Albert Gray
Ranch Hand

Joined: Mar 06, 2001
Posts: 43
madhuri,
The rule is if you want to catch any exception in any catch block, you will have to specify that specific exception in the corresponding "try" body. Line 15: catch(MyException e), MyException is not thrown in the try statement.
cheers,
A

Originally posted by madhuri vl:
class MyException extends Exception{
2. MyException(Object x){}
3. }
4.
5. public class Q19{
6. static void call(){
7. try{
8. try{
9. throw new MyException("");
10. }
11. finally{
12. return;
13. }
14. }
15. catch(MyException e){
16. System.out.println(e.getClass());
17. }
18. }
19.
20. public static void main(String[] args){
21. call();
22. }
23. }
a. Compile time Error at line 9.
b. Compile time Error at line 12.
c. Compile time Error at line 15.
d. Runt time Error at line 15.
e. Run time Error at line 16.
f. Compiles and run successfully.
Can anybody explain?
Thanks in bunches,
Madhuri.

Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
hi all
pls try out the code - it compiles and runs without nay error or exception.
what you missed was that there are two try blocks - hence the return in the inside finally will take the control to the outside try block - where there is catch to complete it.
the inner try block does throw an exception - which will be caught if you have an inner catch block - as you will see in the foll code.
so it means that the exception of class MyException takes a null String as the object req by its constructor - thrown in line 9 of original code.
this exception has no catch block - but has a finally with a return statement - passes control outside.
code :
class MyException extends Exception{
MyException(Object x){}
}

public class Q19{
static void call(){
try{ System.out.println("in outer try");
try{
System.out.println("in inner try");
throw new MyException("");
}

catch(MyException e){
System.out.println("in inner Catch");
System.out.println(e.getClass());
}

finally{
System.out.println("in inner finally");
return;
}

}
catch(Exception e){
System.out.println("in outer Catch");
System.out.println(e.getClass());
}
}

public static void main(String[] args){
call();
System.out.println("in Main");
}
}
Maybe Tony can explain better what happens to the exception which is thrown???
madhuri vl
Ranch Hand

Joined: Jan 17, 2001
Posts: 86
Hi all,
Thank you for the explanation.
hi tony,
Explain means obviously explain the reason of comp error.
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
This code will produce a compile time error in Line 15. The reason is "Exception MyException not thrown in the body for catching". It is a rule that you can code catch only if somebody throws it. Since the inner block catches the Exception the outer block reports a compile error.
Hope it helps --
Hari Gangadharan


<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!
 
Don't get me started about those stupid light bulbs.
 
subject: Natr... mockQ 19