• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Exceptions - Question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following incomplete method.
1. public void method(){
2.
3. if (someTestFails()){
4.
5. }
6.
7.}
You want to make this method throw an IOException if, and only if, the method someTestFails() returns a value of true. Which changes achieve this?
A.Add at line 2: IOException e;
B.Add at line 4: throw e;
C.Add at line 4: throw new IOException();
D.Add at line 6: throw new IOException();
E.Modify the method declaration to indicate that an object of [type] Exception might be thrown.
Answer given is (D), (E).
According to me answer should be (C) & (E).
(E) only if method being referred to is method(), not someTestFails(). Am i right??

Here is a strange thing that i am unable to understand. I tried the following code(though it may appear similar but it has nothing to do with the above question):

output is:
Inside catch block of func2()
Inside func1()
InterruptedException (at line 1) thrown inside catch block of func2 is not being propogated to default exception handler. However if the throws clause from header of main() and func1() is removed, compiler complains.
On the other hand if line 1 and line 2 are interchanged (i.e. instead of throwing exception at line 1, true value is returned and instead of returning a boolean value at line 2, exception is thrown), exception is being propagated to defalut exception handler as expected.
Output is:
Inside catch block of func2()
Exception in thread "main" java.lang.InterruptedException: InterruptedException in catch block of func2()
at Qs.functionfailed(Qs.java:31)
at Qs.withinsomefunc(Qs.java:10)
at Qs.main(Qs.java:5)
Can anybody expalin me why is this happening?
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would expect that:
if (someTestFails())
is the same as:
if (someTestFails() == true).
So, on this basis, C is correct.
As for InterruptedException: By executing a return, the finally block discards the exception that was to be thrown and the code returns normally without even printing a tiny bit of the stack trace.
[ April 13, 2003: Message edited by: Roger Chung-Wee ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think only C is correct.
Your other example is strange - hopefully an expert can help.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a thrown exception is not caught and handled, then it must be declared. The declaration can be the exception or any of its superclasses. These are all valid:
public void method() throws IOException
public void method() throws Exception
public void method() throws Throwable
We are assuming that java.io.IOException was imported.
So, E is correct.
 
Anushkha Rana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx! Roger for the feedback.
It helps!
 
Anushkha Rana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


posted by Martin Smith
I think only C is correct.


Well! Both C and E should be correct as Roger explained:


posted by Roger Chung-Wee
If a thrown exception is not caught and handled, then it must be declared. The declaration can be the exception or any of its superclasses. These are all valid:
public void method() throws IOException
public void method() throws Exception
public void method() throws Throwable
We are assuming that java.io.IOException was imported.
So, E is correct.

 
Martin Smith
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed - it is clear now.
Anybody understand the "bonus" example code?
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Roger's comment, my guess about the example is that if you interchange line1 and line2, fun2 throws exception in finally block. The return value of fun2 will thus be false.
 
pie. 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