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

Exceptions doubt??  RSS feed

 
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

The following is from here

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 answers are given as 2 and 4, 4 is ok, but how come 2 and why not 5?? And will this ever compile??

Confusing.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jothi,
Please use the tag.It is very hard to read and understand the code.
 
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer is : 2 and 4.

Because, what ever exception is thrown or not, finally block will be executed. so, when it reaches finally block, it throws CloneNotSupportedException.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jothi,

2 is fine as we have a catch block that catches IOException & the most important point to notice here that it's a

Checked Exception.Compiler mekes sure that Exceptin is thrown if you had a catch block for the same.

try{
} catch (IOException e){ }

code above will never compile as compiler will flag an error saying " exception java.io.IOException is never

thrown in body of corresponding try statement " . => No throw No catch
so to get your code compile you must have code like given below .

try{
throw newIOException ();
} catch (IOException e){ }

or call any method that migt throw IOException or it's SuperClass providing proper catch block .

again the Exception thrown in try block is overruled by Exception thrown in catch block and finally Exception

thrown in catch block is overruled by Exception thrown in finally block.
so referring said discussion we may conclude.

" If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException".

hope this will help.
Regards
Maneesh Saxena
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!