• 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
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Exception

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javabeat Question

Consider the following code snippet:

void m1() throws Exception{
try {
// line1
}
catch (IOException e) {
throw new SQLException();
} catch(SQLException e)
{ throw new InstantiationException();
} finally {
throw new CloneNotSupportedException() // this is not a RuntimeException.
}}

Which of the following statements are true? Select 2 correct options

(1) If IOException gets thrown at line1, then the whole method will end up throwing SQLException
(2) If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException
(3) If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException()
(4) If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException
(5) If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException()

The given answers are 2 and 4.

But when i run the program with line1 throwing IOException,i got the compile error at catch (SQLException e) . telling SQLException is never thrwon in try.

and also if i run the program with line1 thrown no exception,i got the compile error at catch (SQLException e) and catch (IOException e) .
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva,

Any Checked exceptions can't be thrown with out a try block or declaring method to "throws SQLException" .
But m1().. thrwing Exception right. Then there should be no problem with SQLException
[ March 29, 2007: Message edited by: Srinivasan thoyyeti ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your try-catch block does not contain code, then you cannot catch any checked exception.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the main point of this example was to test your knowledge of what happens if the finally block includes a return or a throw.
[ March 29, 2007: Message edited by: Keith Lynn ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for helping.this is the program,i an running




SQLException and IOException's are subclasses of Exception and the Exception is declared.Then why the error at line1 is coming?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the creators of the question meant that the code could potentially throw a SQLException.

Again, the point of the question what not SQLException or IOException. It was to test to see if you understand what happens when you have a throw or return statement in a finally block.
[ March 29, 2007: Message edited by: Keith Lynn ]
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic