• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Natr... mockQ 19

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explain what?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thank you for the explanation.
hi tony,
Explain means obviously explain the reason of comp error.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic