Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

catch{} blocks

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I come across this question in a java quiz.I think that the answer shouldn't be just C.. but A and D.
I think A is false as we can have many catch {} block from the specific to the least specific.
As for D, it is false too as there's no rules stating that there must be a finally block.
Am I right?
Thanks a lot.
6. Which statement is FALSE about catch{} blocks? a. There must be only one catch{} block in a try/catch structure.

b. The catch{} block for a child exception class must PRECEED that of a parent execption class.

c. The catch{} block for a child exception class must FOLLOW that of a parent execption class.
d. If there is no catch{} block there must be a finally{} block.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
6. Which statement is FALSE about catch{} blocks?
a. There must be only one catch{} block in a try/catch structure.
b. The catch{} block for a child exception class must PRECEED that of a parent execption class.
c. The catch{} block for a child exception class must FOLLOW that of a parent execption class.
d. If there is no catch{} block there must be a finally{} block.
-------------------------------------------------
A is false(you can catch more than 1 exception inside try/catch block) and therefore it's correct answer.
D is true-therefore it's wrong answer
B and C are also false?
Very strange question...
BTW, question asks to select 1 correct answer:
Which statement is FALSE about catch{} blocks?
Where did you find this test?
Jamal Hasanov
www.j-think.com
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question here
http://www.javacommerce.com/tutorial/jbook/Notes/chap80/chap80quiz.html
It asks for only 1 correct answer.
Btw, did I understand you correctly that you say D is true?
My answer is that it should be A,C and D. I am confident that B is correct.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, A is correct(because it's wrong).
I cannot say anything about B&C.
Jamal Hasanov
www.j-think.com
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did this exam and the correct answer is C(???).
You can contact them (by e-mail) and ask the reason....
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the answers would be A and C
I don't think B is right anyways. (it seems like you think B should be false)
I may not be right. Just stating my two cents.
Travis B.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question asks to choose 1 answer: that's why you cannot choose A & C.
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
You may want a third opinion...
I think the answer is A,C,D (these are the FALSE statements)
A is FALSE, because a try/catch block can have more than one catch statements.
B is TRUE because child exceptions must appear before their parent exceptions in a try/catch block (check java spec). This is because child exceptions are more specific, whereas their parents are more generic.
As an example, consider the code below:
import java.io.*;
try{
FileReader fr = new FileReader ("f.abc"); // f.abc doesn't exists
}
catch (FileNotFoundException e) {
System.err.println ("FileNotFoundException");
}
catch (IOException e) {
System.err.println ("IO Exception");
}
This code will compile.
If you try to move the IOException catch block before the FileNotFoundException catch then you will get a compile error. The reason is simple, if the code throwed a FileNotFoundException exception, it will evaluate the catch blocks in sequencial order. That is to say, it will first check whether it is a IOException and if it is not, then would proceed with the second catch. As it is also a IOException, it will then execute that catch block, and will therefore never reach the FileNotFoundExeption catch. In other words, you have an unreachable block of code, which will generate a compile error.
C is FALSE because of the reasons explained in B.
D is FALSE because if you have a code that throws an exception then you must have catch to catch that exception. You cannot replace it with a finally block.

Hope this helps
Eduard
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eduard,
Yes,I agree with you.. that's why I feel that the correct answer should be A,C and D although the question specifically say choose one answer.
That's why I posted the question here.
However,I disagree with the part that if you have a code that throws an exception you must catch it. I believe that it is possible to have a code that throws an exception but don't have a catch block. The catch will be handled by the caller method.
Do pardon me, if I am wrong.
[ May 20, 2002: Message edited by: Karen Leoh ]
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Original post by Eduard Manas:
D is FALSE because if you have a code that throws an exception then you must have catch to catch that exception. You cannot replace it with a finally block.


You can do that OR just put "throws" in the method declaration.
But You can however have a try/finally block that is lacking catch{}
public class TryMe {
public static void main(String args[]) throws Exception {
try {
int i = 0;
}
finally {
System.out.println("No catch needed");
}
}
}
also you can do it without the throws
public class TryMe {
public static void main(String args[]) {
try {
int i = 0;
}
finally {
System.out.println("No catch needed");
}
}
}

It can be try/catch, try/catch/finally, or try/finally.
D was stating that it can never be just try alone, which is true.
Hope this helps,
Travis B.
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did try out the code provided by Travis. Good explanation. To wrap it up, the correct answers should be A and C.
Thanks everybody for clearing up my doubts.
 
Eduard Manas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Travis,
yes you are right on D!
, but you are also wrong (...)

er... I'll explain myself. I agree with you that you can have a try/catch, try/catch/finally, or try/finally.
However, you cannot have a try/finally if the try code throws a checked exception, because you NEED a catch to catch it. In other words, the sentence "d. If there is no catch{} block there must be a finally{} block" is not always true, therefore it should be marked as false.
I think the correct sentence should be: "d.
"When the try block does not throw any checked exception, if there is no catch{} block there must be a finally{} block"
What you think guys?
Karen, regarding to your point of not all exceptions must be caught, you are right. You don't need to catch RuntimeExceptions. I didn't mentioned it for simplicity reasons. Sorry if I confused you.
Eduard
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you on the fact about checked exceptions, but that is an IF case. IF it happens, then you need try/catch.
I think D was just testing you on your knowledge of if try exist withour catch.
Anyways, this question,wherever it was found. I deem it too ambiguos for my studying habits. The question could have (and should have) been laid out much better. As far as asking for one answer, Karen, does it actually ask for just one? many mock exams, including Marcus Green, will not tell you how many to choose. You must decide that for yourself.
Hope this helped,
Travis B.
 
Karen Leoh
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Travis, the question really ask for just one answer(options are displayed using radio buttons).
Anyway, I agree with Travis again that the question is ambigious.
Thanks again everybody!
 
reply
    Bookmark Topic Watch Topic
  • New Topic