Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Exception  RSS feed

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
If your try-catch block does not contain code, then you cannot catch any checked exception.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!